JavaScript Hidden Guide !!
Overriding & Overloading
JavaScript does not support overloading.
JavaScript supports overriding, so if you define two functions with the same name, the last one defined will override the previously defined version and every time a call will be made to the function, the last defined one will get executed.
Arguments Object
function test(one,two){
console.log(arguments);
//old way
var args = Array.prototype.slice.call(arguments);
//new way
const argss = Array.from(arguments);
//or spread
var args = […arguments];
let t = one || ‘one’;
let te = two || ‘two’;
console.log(t);
console.log(argss)
}
test(1);
package.json Hidden Thought
“core-js”: “2.4.1”,
(2:major ,4:minor,1:Patch)
Default/Option Parameter
function addressFunction (address, city, state, country) {
country = country || “US”; // if country is not passed, assume USA
//rest of code
}
JS Loosly typed
Javascript is loosely typed, except in switch statements. JavaScript is NOT loosely typed when it comes to case comparisons.
var myVar = 5;
if (myVar == ‘5’) {
// returns true since Javascript is loosely typed
alert(“hi”); //this alert will show since JS doesn’t usually care about data type.
}
switch(myVar) {
case ‘5’: alert(“hi”); // this alert will not show since the data
undefined vs null
typeof(undefined) => undefined
typeof(null) = object
Shallow Copy Vs Deep Copy
var copy = origional;
vs
var copy = JSON.parse(JSON.stringify( original ));
JS The right way
Prototyping Chain
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
Currying in JS
What is curring: Curring is the partial invocation of a function. Currying means first few arguments of a function are pre-processed and a function is returned. The returning function can add more arguments to the curried function. It’s like if you have given one or two spice to the curry and cooked a little bit, still you can add further spice to it. A simple example will look like-
function addBase(base){
return function(num){
return base + num;
}
}
var addTen = addBase(10);
addTen(5); //15
addTen(80); //90
addTen(-5); //5
function mul(x){
return function m(y){
return x*y;
}
}
var test = mul(5);
console.log(test(3));
Closure inside loop
for(var i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i);
}, 10);
}
Explanation: The console log is inside the anonymous function of setTimeout and setTimeout is executed when the current call stack is over. So, the loop finishes and before setTimeout get the chance to execute. However, anonymous functions keep a reference to I by creating a closure. Since, the loop is already finished, the value i has been set to 10. When setTimeout use the value of i by reference, it gets the value of i is 10. Hence, you see 10 ten times.
JavaScript execution way!!
Question: What is the value of “1”+2+4
Answer: “124”. For this one “1” + 2 will produce “12” and “12”+4 will generates “124”.
What is the value of +’dude’
Answer: NaN. The plus (+) operator in front of a string is an unary operator that will try to convert the string to number. Here, JavaScript will fail to convert the “dude” to a number and will produce NaN.
Rev string in JS
let str = “i am alok”;
let rev = str.split(“ “).reverse().join(“ “);
console.log(rev);
output :alok am i
let rev = str.split(“”).reverse().join(“”);
when space is removed then o/p : kola ma i
Find Mising number from array
let arr= [1,2,3,4,5,7,8,9,10];
let len=arr.length;
let total = 10*(10+1)/2
let t = arr.reduce((a,c)=>a+c);
console.log(total-t)
0/p=6
Just Sort array
let arr= [1,2,7,4,5];
arr.sort((a,b)=>a-b);
arr.sort((a,b)=>a<b);
console.log(arr)
DATATYPES in JS
According to the latest ECMAScript release, these are the data types:
Boolean
Null
Undefined
Number
String
Symbol
Object
JavaScript is Call by Value or Call by Reference???
JavaScript always passes by value. However, if you pass an object to a function, the “value” is really a reference to that object, so the function can modify that object’s properties but not cause the variable outside the function to point to some other object.
var a = [“1”, “2”, {foo:”bar”}];
var b = a[1]; // b is now “2”;
var c = a[2]; // c now references {foo:”bar”}
a[1] = “4”; // a is now [“1”, “4”, {foo:”bar”}]; b still has the value
// it had at the time of assignment
a[2] = “5”; // a is now [“1”, “4”, “5”]; c still has the value
// it had at the time of assignment, i.e. a reference to
// the object {foo:”bar”}
console.log(b, c.foo); // “2” “bar”
Function & Object declaration ways:
If you look at ECMA script specification, there are 3 ways you can define a function. (Page 98, Section 13 Function Definition):
1:Function constructor
2:Function declaration
3:Function Expression
7 Ways to create object in Javascript:
1:Object constructor
2:Object.create()
3:{}
4:Function constructor
5:function Constructor+prototype
6:Singalton
7:es6 syantax
— — — — — — — — — — — — —