IIFE(Immediately Invoked Function Expression)
An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.
Here we are going to write IIFE by 12 ways.
1. | |
(function() { | |
console.log("I am called"); | |
})(); | |
2. | |
(function() { | |
console.log("I am called"); | |
}()); | |
3. | |
~function() { | |
console.log("I am called"); | |
}(); | |
4. | |
!function() { | |
console.log("I am called"); | |
}(); | |
5. | |
+function() { | |
console.log("I am called"); | |
}(); | |
6. | |
-function() { | |
console.log("I am called"); | |
}(); | |
7. | |
[function() { | |
console.log("I am called"); | |
}()]; | |
8. | |
const iife = function(){ | |
console.log("I am called") | |
}(); | |
9. | |
true && function(){ | |
console.log("I am called") | |
}(); | |
10. | |
0, function(){ | |
console.log("I am called") | |
}(); | |
11. | |
new function(){ | |
console.log("I am called") | |
} | |
12. | |
new function(){ | |
console.log("I am called") | |
}(); |
Creating Arrays
The JavaScript Array object is a global object that is used in the construction of arrays; which are high-level, list-like objects. For this topic I would like to say thanks to 2ality.com .
Here we are going to create array by 10 ways.
1. new Array(2) | |
2. Array.from({length : 2}) | |
3. [… new Array(2)] | |
4. let arr =[]; | |
for(var itr=0; itr<2; itr++){ | |
arr.push(0) | |
} | |
5. new Array(2).fill(0) | |
6. Array.from({length : 2}, () =>({})) | |
7. Array.from(new Array(2)) | |
8. Array.from(new Array(2), ()=> 0) | |
9. Array.from(new Array(2), (x,i)=> i); | |
10. [… new Array(2).keys()] |
Define Function
Functions are one of the fundamental building blocks in JavaScript. A function is a JavaScript procedure — a set of statements that performs a task or calculates a value. To use a function, you must define it somewhere in the scope from which you wish to call it.
Here we can write function by 6+ ways.
1. function add(a,b){ | |
return a+b | |
} | |
2. let add = function(a,b){ | |
return a+b | |
} | |
3. let add = (a,b) =>{ return a+b } | |
4. let add = new Function('a','b', 'return a+b' ) | |
5. function* add(item) { | |
yield item; | |
yield item+ 10; | |
} | |
var gen = add(10); | |
6. | |
function *add(item) { | |
yield item; | |
yield item+ 10; | |
} | |
var gen = add(10); |
Covert to Number
The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor. A primitive type object number created using the Number() function.
Here we can convert string to Number by 9 ways.
var a = "10"; | |
1. console.log(+a); | |
2. console.log(Number(a)); | |
3. console.log(1*a); | |
4. console.log(a-0); | |
5. console.log(parseFloat(a)); | |
6. console.log(parseInt(a)); | |
7. console.log(Math.floor(a)); | |
8. console.log(Math.round(a)); | |
9.console.log(~~a); | |
Note :- | |
(i) +, Number and * is fast compare to all. | |
(ii) Method 6-9 is not applicable when value in decimal. i.e. ("10.11"). |
Top comments (0)