Truthy and Falsy
There are some values in JavaScript that represent Truthy, and some represent falsy.
Truthy Values
- Any Number except 0
- Any String except empty string (“”)
- Boolean true
- Any Array and Object also includes empty Array and Object
Falsy Value
- Number 0
- Empty String
- Boolean false
- Null
- Undefined
- NaN
Null vs. Undefined
Null and Undefined are primitive values in JavaScript. Both are almost the same but there is a minor difference between them. Undefined value comes automatically, but the Null value never comes automatically. If you don’t set the value of a variable, you will find an Undefined value. On the other hand, you have to manually set the Null value if you need it. Null value means there is no value.
Double equal vs. triple equal
To compare two values in JavaScript you have to use a comparison operator. To compare, you can use either double equal (==) or triple equal (===). Both functions dose the same job but in a different way. In simple words, double equal don’t care about the data type. But triple equal care about the value and also the type of value.
Map vs. Filter
In JavaScript array Map is almost like the for loop. It is a built-in function in JavaScript. It takes three parameters, element, index, Array. The first one is required, and others are optional. It returns a new Array.
Filter vs. Find
Filter and Find are built-in functions. It is used in Array. Filter and Find both are almost the same. The main difference between them is the return type. Filter returns an Array, and Find return a single value. That means if there is no result, then the Filter will return an empty array.
Closure
The closure is like a closed environment. When a function returns another function and the inner function uses a variable from its parent function, then the inner function calculates the variables value independently.
Array split
In JavaScript you can slice an array. Slice is a built-in function. It takes two parameters, starting point, and ending point. It returns a new Array but doesn’t affect the original Array.
const number = [ 5, 7, 10, 15 ];
const newNumber = number.slice(1, 3);console.log(newNumber);
//Expected output is [7, 10]
Join
Join is also a built-in function. It joins all elements of an array. It takes only one parameter.
const number = [1, 2, 3, 4, 5, 6];
const join = number.join("");
const joinWithSpace = number.join(" ");
const joinWithComa = number.join(", ");console.log(join);
//Expected output is 123456console.log(joinWithSpace);
//Expected output is 1 2 3 4 5 6console.log(joinWithComa);
//Expected output is 1, 2, 3, 4, 5, 6
Break Continue
Break and Continue is a keyword in JavaScript. Break just break a loop and Continue ignore the code below it.
const number = [0, 1, 2, 3, -4, 5, 6]
let output = [];for(let i = 0; i < number.length; i++){
if(number[i] < 0){
break;
}
output.push(number[i]);
}console.log(output);
//Expected output is [0, 1, 2, 3]
const number = [0, 1, 2, 3, -4, 5, 6]
let output = [];for(let i = 0; i < number.length; i++){
if(number[i] < 0){
continue;
}
output.push(number[i]);
}console.log(output);
//Expected output is [0, 1, 2, 3, 5, 6]