-
By default “list, object, function” copy works like a reference copy. This means, that if B is a copy of list A then, any changes made to B will also affect A.
var arr = [1, 2, 3]; var copyArr = [1, 2, 3]; copyArr.pop(); // Now, arr = [1, 2]
How to prevent that? Well, we can use spread operator.// Object example var obj = {name: "Sabit"}; var copyObj = {...obj}; // Array example var arr = [1, 2, 3]; var copyArr = {...arr};
-
List operations:
arr.pop(); // removes last eleme arr.push(10); // add elems at back arr.shift(); // removes the 1st item arr.unshift(50); // add elements to the front
-
Hoisting:
Can use variables before declaringconsole.log(a); // Output: undefined, but works var a = 10;
-
Versions: Mainly 2 versions of JS.
- ES5
- ES6
-
Var, let, const:
- “var” (ES5 version)
- “var, let, const” (ES6 version)
-
“Var” is function scoped
function foo () { for (var i = 0; i < 3; ++i) { console.log(i); } console.log(i); // Here "i" can still be used } // Output: 0, 1, 2, 3
-
“Let and const” is braces scoped
function foo () { for (let i = 0; i < 3; ++i) { console.log(i); } console.log(i); // throws error } // Output: 0, 1, 2, ERROR
-
“Var” adds itself to the window object.
-
“Let, const” doesn’t add itself to the window object.
-
Some features are not present in JS. But, we can still use them. Which are provided by the browsers we use. All of those features are provided in the window object.
-
Truthy & Falsy:
- Falsy values - {0, false, undefined, null, NaN, document.all}
- Truthy values - everything else is truthy.
-
forEach loop:
Only runs on array. Works on the temporary copy of the array. That's why items of the array are not changed.var a = [1, 2, 3, 4]; a.forEach(function (item) { console.log(item + 7); // Doesn't changes the actual array })
-
for-in loop is only used for objects.
var obj = { name: "Sabit", age: 23, city: "Dhaka" } for (var key in obj) { console.log(key); // Outputs the keys only console.log(obj[key]); // Outputs the value of the corresponding key }
-
Callback function:
If a code-block, runs after a particular time limit, we write that code-block into a function and that function is callback function. This is a part of Asynchronous Javascript.setTimeout(function () { console.log("I'm a callback function, because I'm running after a particular time limit."); }, 2000);
-
First class function:
If a function is treated as a value. Then, it is called a first class function.function foo (firstClass) { firstClass(); // Output: I'm a first class function } foo(function () { console.log("I'm a first class function"); }) // Here, this anonymous function is passed as a value to another function.
-
Arrys in Javascript are not actually arrays. They're actually objects.
var A = [10, 20, 30]; console.log(typeof A); // Output: Object
Behind the scene, JS converts the array like this in {index: value} pairs:
// Behind the scene: var A = { 0: 10, 1: 20, 2: 30 }
As arrays are not actually arrays but objects, so we can even store items in negative index as follows:
A[-1] = 999; // A = [10, 20, 30, -1: 999]
-
Delete a key from object:
var obj = { name: "Sabit", age: 23 } delete obj.age; // Output: obj = {name: "Sabit"}