What happens when you need a literal quote: " or ' inside of your string?
// escape a quote by placing a backslash (\) in front of the quote.
const sampleStr = 'Alan said, "Peter is learning JavaScript".';
console.log(sampleStr); //Alan said, "Peter is learning JavaScript".
Concatenate a string onto the end of an existing string variable.
let ourStr = 'I come first. ';
ourStr += 'I come second.';
console.log(ourStr); //I come first. I come second.
append variables to a string using the plus equals (+=) operator.
const anAdjective = 'awesome!';
let ourStr = 'Metacrafters is ';
ourStr += anAdjective;
console.log(ourStr); //Metacrafters is awesome!
If you have multiple inputs with the same output, you can represent them in a switch statement like this:
let result = '';
switch (val) {
case 1:
case 2:
case 3:
result = '1, 2, or 3';
break;
case 4:
result = '4 alone';
}
Cases for 1, 2, and 3 will all produce the same result.
You can also chain them together to check for multiple conditions.
just like if, else if, and else statements to check multiple conditions
Instead of;
function findGreaterOrEqual(a, b) {
if (a === b) {
return 'a and b are equal';
} else if (a > b) {
return 'a is greater';
} else {
return 'b is greater';
}
}
do this instead
function findGreaterOrEqual(a, b) {
return a === b
? 'a and b are equal'
: a > b
? 'a is greater'
: 'b is greater';
}
return a boolean true or false value.
instead of ;
// returns true or false
function isEqual(a, b) {
if (a === b) {
return true;
} else {
return false;
}
}
Do this ;
// returns true or false
function isEqual(a, b) {
return a === b;
}
const ourArray = [];
for (let i = 0; i < 10; i += 2) {
ourArray.push(i);
}