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 a global variable | |
var globalVar = "abc"; | |
// self invoking parent/outer function | |
(function outerFunction (outerArg) { // begin scope of outerFunction | |
// declare a variable in outerFunction scope | |
var outerFuncVar = 'x'; | |
// self-invoking closure function - inner function | |
(function innerFunction (innerArg) { // begin of scope innerFunction | |
// variable declared in innerFunction scope |
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 a function | |
function foo() { | |
// this refers to the reference over which this function is calling | |
console.log( this.bar ); | |
} | |
// declare a global variable bar | |
var bar = "global"; | |
// declare an object with 2 key-value |
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 a conterFunc | |
function counterFunc() { | |
// declare a private var | |
var counterVar = 0; | |
// return an object, with functions add & get that allows us | |
// to modify the private variable `counterVar` | |
return { | |
add: function(increment) { counterVar += increment; }, | |
get: function() { return `The value of counterVar is ${counterVar}` } | |
} |
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
// Given two strings | |
var firstWord = "Mary"; | |
var secondWord = "Army"; | |
// call a function to check if given two words are Anagrams | |
isAnagram(firstWord, secondWord); // Output is: true | |
function isAnagram(first, second) { | |
// For case insensitivity, change both words to lowercase. | |
var a = first.toLowerCase(); |
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
// iterate for-loop 100 times | |
for (let i = 1; i <= 100; i++) { | |
// check if `i` is isFizz - that is multiples of 3 | |
const isFizz = i % 3 == 0; | |
// check if `i` is isBuzz - that is multiples of 5 | |
const isBuzz = i % 5 == 0; | |
// print output: if it's isFizz there could be two possibility 'Fizz' or 'FizzBizz' and | |
// if it's not isFizz then it will be 'isBuzz' or nothing | |
console.log(isFizz ? (isBuzz ? 'FizzBuzz' : 'Fizz') : isBuzz ? 'Buzz' : i); |
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
function duplicate(arr) { | |
// to duplicate an array use concat an array | |
return arr.concat(arr); | |
} | |
// call a function to duplicate array | |
const output = duplicate([1, 2, 3, 4, 5]); | |
console.log(output) // Output: [1,2,3,4,5,1,2,3,4,5] |
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
function isInt(num) { | |
// check if remainder left when we divide by 1 | |
return num % 1 === 0; | |
} | |
// call isInt function | |
console.log(isInt(4)); // true | |
console.log(isInt(12.2)); // false | |
console.log(isInt(0.3)); // false |
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 arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created array | |
var anotherArrayList = arrayList; // Referenced arrayList by another variable | |
arrayList.length = 0; // Empty the array by setting length to 0, this will empty all reference | |
console.log(anotherArrayList); // Output [] |
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 arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created an array | |
var anotherArrayList = arrayList; // Referenced arrayList by another variable | |
arrayList = []; // Empty an arrayList | |
console.log(anotherArrayList); // anotherArrayList remains same, that is still pointing to old reference | |
// Output ['a', 'b', 'c', 'd', 'e', 'f'] |
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 arrayList = [1 , 2, 3]; | |
// check if arrayList is an Array or not | |
const isArr = Array.isArray(arrayList); | |
console.log(`Input arrayList is Array: ${isArr}`) |