You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Declare variablesvarfirst_name="Gia Phuc";varlast_name="Pham";console.log(first_name+last_name);// Gia PhucPhamconsole.log(first_name+" "+last_name);// Gia Phuc Pham
OR:
// Declare variablesvara="Hello";a=a+" World";console.log(a);// Hello World
Comparison: compare strings alphabetically
// Declare variablesvarletter_a="A";varletter_b="B";console.log(a>b);// true (Because in the alphabet, B is after A.)// Declare variablesvarnum1="20";varnum2="5";console.log(num1>num2);// false (Because in number, 5 is larger than 2.)
5. If-else Condition
Syntax:
// If statementif(condition1){action1;// false}
OR:
// If-else statementif(condition1){action1;// true}else{action2;// false}
OR:
// Multiple or Nested if statementsif(condition1){if(condition2){action1;}else{action2;}}
Notice: There are 6 variables considered a false boolean.
Variables
0
false
"-"
undefined
NaN = Not a number
null
// Declare variablesvara=0;varb=1;if(a){// because a == 0 -> false -> Line 2console.log("Line 1");}else{console.log("Line 2");}if(b){// because a != 0 -> true -> Line 1console.log("Line 1");}else{console.log("Line 2");}
6. Logical Operators = Boolean Operators
Operators
Description
Operation
&&
logical and
Returns the result of the last elements if there is not any falsy in the comparison, otherwises, it will return falsy elements
II
logical or
Returns the result of the first and not falsy elements in the comparison
Truthy = to bool is true: Any value when converting to a boolean data type that has the value true, we call that value Truthy.
Falsy = to bool is false: Any value when converting to a boolean data type that has a value of false, we call that value Falsy.
Notice: There are 6 variables considered Falsy. Besides these variables, the remainings are considered Truthy
Variables
0
false
"-"
undefined
NaN = Not a number
null
Exception: document.all
In Javascript (browser side) there will be a document object available, and when you try !!document.all will return false. Is document.all falsy too?
I wondered this myself, so I googled "Why document.all is falsy?" And I found the answer here.
Summary of the answer:
document.all is a single official exception according to the ECMA specification (version 5). This specification describes all objects that when converted to boolean will be true. However, document.all is an exception.
As follows:
document.all converted to boolean will be false
document.all as operand of comparison operator == or != will be undefined
When typeof document.all will return "undefined"
ECMA is a specification that the languages under this specification must
follow. Javascript is a language that complies with the ECMA specification.