Javascript
/
Fundamentals
- 1 Fundamentals 11
-
Hello World S
-
Code structure S
-
Use strict S
-
Variables S
-
Data types S
-
Type conversions S
-
Maths S
-
Comparitions S
-
Conditional S
-
Loops S
-
Functions S
- 2 Testing 2
-
Mocha S
-
Nested describe S
- 3 Objects 4
-
Basics S
-
Reference S
-
Methods S
-
Constructor S
- 4 Types 5
-
Primitives S
-
Numbers S
-
Strings S
-
Arrays S
-
Json S
- 5 Classes 3
-
Constructor S
-
Binding S
-
Inheritance S
S
R
Q
Javascript Fundamentals Conditional
The if statement converts the result to a boolean The ternary operator has three operands OR return the first truly one if (age > 18) {} b = a > 18 ? Yes : No (1 || 0) === 1 // trueTernary
Boolean
The if statement evaluates the expression in its parantheses.
/**
* The if statement converts the result to a boolean
* If the result is true executes the block of code
*
* The ternary operator has three operands
* It's the only operator witch has more than two operands
*
* The two question marks operator ?? nullish coalescing
* returns the first argument if it's not null/undefined
*/
let a = 20;
let x;
let b = a > 18 ? 'Yes' : 'No'; // Yes
let y = x ?? a; // 20
if (a > 18) {
console.log("Allowed"); // Allowed
}
console.assert(b === 'Yes'); // pass
console.assert(y === 20); // pass
Logical
In Javascript OR / AND have some powerful uses.
/**
* In Javascript the OR operator is a bit trickier
* If an operand is not boolean, it's converted to a boolean
* If all operands have been evaluated false, returns the last operand
*
* OR return the first truly one
* AND returns first falsy value
*/
console.log( (true || false) === true ); // true
console.log( (false || false) === false ); // true
console.log( (1 || 0) === 1 ); // true
console.log( (null || false || '0') === '0'); // true
let firstName;
let nickName;
let usename = firstName || nickName || 'Anonymous';
console.log(usename); // Anonymous
console.log( 1 && 0); // 0
console.log( 0 && 'abc'); // 0
➥ Questions