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
var sum = function(a, b){ | |
var res = a + b; | |
return res; | |
} | |
sum(5,10); | |
// 15 | |
var sumDefault = function(a, b = 10){ | |
var res = a + b; | |
return res; | |
} |
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
// normal funciton | |
const double = function(num){ | |
return num*2; | |
} | |
// converted to arrow function | |
num => num * 2 | |
num => num * 2 | |
[1,2,3].map(num => num * 2); | |
// [2, 4, 6] |
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
// This is a single line comment | |
/* This is a comment spanning multiline. | |
You should be careful about writing comment. */ | |
var num1 = 50; | |
var num2 = 50; |
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
// This is a single line comment | |
/* This is a comment spanning multiline. | |
You should be careful about writing comment. */ | |
var num1 = 50; | |
var num2 = 50; |
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
// error free code | |
try { | |
alert("Hello World"); | |
} catch (err) { | |
alert("Error found"); | |
} | |
// code with error | |
try { |
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
console.log(typeof(19)); | |
// number | |
console.log(typeof(undefined)); | |
// undefined | |
typeof("Hello World!"); | |
"string" | |
console.log(typeof true); | |
// boolean | |
console.log(typeof {}); | |
// object |
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
// constants | |
console.log(Math.PI) | |
// 3.141592653589793 | |
console.log(Math.LN2) | |
// 0.6931471805599453 | |
Math.sqrt(9) | |
// 3 | |
Math.pow(3, 2) | |
// 9 | |
Math.abs(-9) |
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
const year = 2020 | |
var result; | |
if (year>2019){ | |
result = "Yes"; | |
} else{ | |
result = "No"; | |
} | |
console.log(result) | |
//Yes | |
var result = year > 2019 ? "Yes" : "No"; |
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
var name = " JavaScript"; | |
var language = " JavaScript "; | |
console.log(language); | |
// JavaScript | |
var languageBoth = language.trim(); | |
console.log(languageBoth) | |
// JavaScript | |
var languageStart = language.trimStart(); | |
console.log(languageStart) | |
// JavaScript |
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
// declare an array | |
var fruits = ["apple", "orange", "banana", "pineapple"] | |
// loop over with items | |
fruits.forEach(function(fruit){ | |
console.log(fruit); | |
}) | |
// output | |
// apple | |
// orange | |
// banana |