This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Truthy/Falsy value */ | |
let age = 0; | |
if (age) { | |
console.log(true); | |
} else { | |
console.log(false); | |
} | |
// Output : false | |
let name; // undefined, falsy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* unassigned variable */ | |
let name; | |
console.log(name); | |
// Output: undefined | |
/* No return/ nothing after return */ | |
function add(num1, num2) { | |
result = num1 + num2; | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 👎 double equal == */ | |
const first = 1; | |
const second = "1"; | |
if (first == second) { | |
console.log(true, `${first} == ${second}`); | |
} else { | |
console.log(false, `${first} != ${second}`); | |
} | |
// Output : true 1 == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global/block scope */ | |
let result = 10; | |
function sum(x, y) { | |
let result = x + y; | |
console.log(result); // 35 | |
if (result > 10) { | |
var msg = "more than 10"; | |
console.log(msg); | |
} | |
console.log(msg); // outside scope, because of hoisting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Map */ | |
const numbers = [2, 3, 4, 5, 6, 8]; | |
const squaredNum = numbers.map((num) => num * num); | |
console.log(squaredNum); | |
// Output : [ 4, 9, 16, 25, 36, 64 ] | |
/* Filter */ | |
const shapes = ["🟨", "🟩", "🔵", "🔴", "🟨", "🟩", "🔵", "🔴"]; | |
const circle = shapes.filter((item) => item === "🔴"); | |
const square = shapes.filter((item) => item === "🟩"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* slice(), splice(), join() */ | |
const sports = ["⚽️", "🏀", "🏈", "🏓", "🏸", "🏏", "🏐", "🎱"]; | |
const part = sports.slice(3, 6); | |
console.log(part); | |
// Output: ["🏓", "🏸", "🏏"] | |
const removed = sports.splice(2, 6, "🎭", "🤹"); | |
console.log(removed); | |
console.log(sports); | |
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* let, const */ | |
let gf = "Jennifer Lawrence"; | |
console.log(gf); | |
gf = "Elizabeth Olsen"; | |
console.log(gf); | |
/** | |
* Output : | |
Jennifer Lawrence | |
Elizabeth Olsen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 3 dots: spread */ | |
const red = ["🧡"]; | |
const green = ["💚"]; | |
const blue = ["💙"]; | |
const rgbHeart = [...red, ...green, ...blue]; | |
console.log(rgbHeart); | |
// Output : ["🧡", "💚", "💙"] | |
/* destructure: array */ | |
const hearts = ["🧡", "💚", "💙", "💛", "💜", "🖤", "🤍"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Default Parameter */ | |
function addFifty(num1, num2 = 50) { | |
return num1 + num2; | |
} | |
const result = addFifty(10, 20); | |
const result2 = addFifty(20); | |
console.log(result, result2); | |
// Output : 30 70 | |
/* Template string */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Arrow functions - implicit return*/ | |
const doubleIt = (num) => num * 2; | |
console.log(doubleIt(4)); | |
// Output: 8 | |
const add = (a, b) => a + b; | |
console.log(add(40, 50)); | |
// Output: 90 | |
/* explicit return */ |
OlderNewer