Sunday, 16 December, 2018 UTC


Summary

This is my 3rd post for the series “Just so you know” where we are going to unlock deeper knowledge about JavaScript codes.
So without further ado. Let’s proceed to our first code.
 
NaN (Not A Number) is a type of number.
Crazy right? even though NaN represents a “Not-a-Number” value, its a special value in which results from a operation that has a non-numeric operands. For Example 1 * 'a'
const operation = 1 * 'a'; // 1 times a

console.log(operation); // returns NaN
console.log(typeof operation); // returns number
 
Null is an object.
To summarize this, null is an assigned value which means nothing. On the other hand undefined is a declared variable but not defined yet. So be careful if you have an if statement that detects whether a value is an object since null is one of them.
console.log(typeof null); // object
console.log(null === undefined); // false
console.log(null == undefined); // true
 
Access more than expected function parameters
Basically we could use the arguments variable inside our function to get access to all variables that was passed in our function.
function sum(num1, num2) {
    return Array.from(arguments).reduce((acc, cur) => acc + cur);
}

console.log(sum(1, 2)); // returns 3
console.log(sum(1, 2, 3, 4, 5)); // returns 15
 
Array reverse() method
As we all know that reverse method return the array in reverse order. However the interesting part is that it reverse also the order of the original array and it returns itself so whatever you do with the assigned variable will also happen to the original array.
const arr1 = [1, 2, 3, 4, 5];
const arr2 = arr1.reverse();

console.log(arr1); // returns [ 5, 4, 3, 2, 1 ]
console.log(arr2); // returns [ 5, 4, 3, 2, 1 ]

arr2.pop();

console.log(arr1); // returns [ 5, 4, 3, 2 ]
console.log(arr2); // returns [ 5, 4, 3, 2 ]
 
Unary plus (+)
Unary + operator converts its operand to Number type. shorthand for NumberparseInt, parseFloat
const strNumber = '10';
const string = 'a';
const boolTrue = true;
const bootFalse = false;

console.log(+strNumber);    // 10
console.log(+string);       // NaN
console.log(+boolTrue);     // 1
console.log(+bootFalse);    // 0
console.log(+null);         // 0
console.log(+undefined);    // NaN

console.log(10 + '20');     // 1020
console.log(10 + +'20');    // 30
 
So that’s all for this post. Stay tune as a lot of this will be posted every monday and thursday.
 

For more “Just so you know” posts. Just click here