Nathan, 1. may I know what the name of Javascript syntax in there? because as I know ES6 require semicolon (;) 2. in the last section, you clean up your if else code with ternary. So basically, can we use ternary to replace if else code? if no, when we should use if else instead of ternary? thanks, just discovered your channel and it is AMAZING!!
Wow, thanks! So, number 1 - the syntax is totally legal JavaScript. JavaScript was designed early on to be an 'approachable' language, and one decision that was made was to include Automatic Semicolon Insertion (ASI). This means that the JS engine actually adds semicolons for you. And because removing ASI from the language would break massive chunks of the internet, it has to stay in there. As a result, there are groups of people (including myself) who choose to only use semicolons where they're needed. For example, when putting multiple expressions on the same line: let x = 0; return x + 1 And the linter I'm using is Standard, if you're curious about any other style choices I made here. And number 2 - yeah, a ternary evaluates the expression before the question mark. If it's truthy, the expression after the question mark is executed. Otherwise, the third statement is executed. Chaining ternaries creates an 'if __ then __ else if __ then __ otherwise __' statement. I like using that in place of an if/else that only has a return statement inside of each block since the if/else syntax is pretty cluttered. But the great thing about ternary statements is that you can't have an 'if' without an 'else' Check out my What the Heck is a Ternary video if you are curious about them. Let me know if you have any further questions.
Nathan,
1. may I know what the name of Javascript syntax in there? because as I know ES6 require semicolon (;)
2. in the last section, you clean up your if else code with ternary. So basically, can we use ternary to replace if else code? if no, when we should use if else instead of ternary?
thanks, just discovered your channel and it is AMAZING!!
Wow, thanks!
So, number 1 - the syntax is totally legal JavaScript. JavaScript was designed early on to be an 'approachable' language, and one decision that was made was to include Automatic Semicolon Insertion (ASI). This means that the JS engine actually adds semicolons for you. And because removing ASI from the language would break massive chunks of the internet, it has to stay in there. As a result, there are groups of people (including myself) who choose to only use semicolons where they're needed. For example, when putting multiple expressions on the same line:
let x = 0; return x + 1
And the linter I'm using is Standard, if you're curious about any other style choices I made here.
And number 2 - yeah, a ternary evaluates the expression before the question mark. If it's truthy, the expression after the question mark is executed. Otherwise, the third statement is executed.
Chaining ternaries creates an 'if __ then __ else if __ then __ otherwise __' statement.
I like using that in place of an if/else that only has a return statement inside of each block since the if/else syntax is pretty cluttered.
But the great thing about ternary statements is that you can't have an 'if' without an 'else'
Check out my What the Heck is a Ternary video if you are curious about them.
Let me know if you have any further questions.
why 7 adds to 27 instead of 19 how it knows that the value is 27 and where is it stored(27)?
const words = ["I", "know", "recursion"];
console.log(joinWords(words));
function joinWords([firstWord, ...otherWords]) {
if(arguments[0].length === 0) return "!"
else return firstWord + " " + joinWords(otherWords);
}