Created
May 21, 2021 08:10
-
-
Save junaidtk/87d6cafb29c140d0f85297447e311315 to your computer and use it in GitHub Desktop.
Javascript feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Arrow Function | |
=============== | |
In arrow function we don't need to use the key word: function | |
if the function contain only contain one single line, then there is no need for using the opening body bracket({}) and return key word. | |
if the function has only one argument, there is no need of argument paranthesis. | |
eg: | |
const sum = (a, b)=> { | |
return a+b; | |
} | |
console.log(sum(5, 7)) // output, 12 | |
const multiply = (a, b) => a*b | |
console.log(multiply(5, 7)) // output, 35 | |
const double = a => a*2 | |
console.log(double(5)) // output, 10 | |
2. Default parameter: | |
===================== | |
From ES6 onwards, the javascript can define the default parameter in the function. | |
The default parameter value will use when an undefined value is passed to function or didn't pass any parameter to function. | |
eg: | |
function add(a, b=5){ | |
return a+b; | |
} | |
console.log(add(7)) // output, 12 | |
function add(a, b=5){ | |
return a+b; | |
} | |
console.log(add(7, 4)) // output, 11 | |
3. Immediately Invoked Function Expression (IIFE) | |
=========================================== | |
If we defined a function and we need to call it immeadiatley. This is called IIFE function. It is a anonymous function. | |
we can't call this function another time. | |
eg: | |
(function (){ | |
console.log(5+7); | |
})(); // output, 12 | |
4. Spread operator(...) | |
======================= | |
You can concat array, object, and string by using the spread operator. | |
It is used for array expression or string to be expanded or an object expression to be expanded in places. | |
eg: | |
const numbers = [1, 8, 5, 15, 10]; | |
consloe.log([...numbers]) // output, [1, 8, 5, 15, 10] | |
consloe.log([...numbers, 65]) // output, [1, 8, 5, 15, 10, 65] | |
const user = {name: 'Shuvo'} | |
consloe.log({...user}) // output, {name: 'Shuvo'} | |
consloe.log({...user, id: '1'}) // output, {name: 'Shuvo', id: '1'} | |
5. Data types: | |
============== | |
1. Primitive data type: Number, string, boolean, undefined, null, symbol, Bigint. | |
2. Reference data type: object(array, regular expression, date) and function. | |
6. Destructuring: | |
================== | |
We can destruture array and object in javascript into variable. | |
Object: | |
Varaible name and Property name should be same. | |
Should not maintain order which one first or last. | |
Declare variable in {} bracket. | |
Array: | |
No need of same name for variable name. | |
Shuold maintain order. we should write elements by an order which element was first or last or any position. | |
If we want to acces second element, don't want first element, use comma on that place. | |
eg: | |
// array destructuring | |
cosnt [num1, num2] = [5, 10]; | |
console.log(num1, num2) // output, 5, 10 | |
cosnt [, num2] = [5, 10]; | |
console.log(num2) // output, 10 | |
cosnt [num1, ...other] = [5, 10, 15]; | |
console.log(num1, other) // output, 5, [10, 15] | |
// object destructuring | |
cosnt {num1, num2} = {num1:5, num2:10}; | |
console.log(num1, num2) // output, 5, 10 | |
cosnt {num2, num1} = {num1:5, num2:10}; | |
console.log(num2, num1) // output, 10, 5 | |
cosnt {num2} = {num1:5, num2:10}; | |
console.log(num2) // output, 10 | |
cosnt {num1, ...other} = {num1:5, num2:10, num3: 56}; | |
console.log(num1, other) // output, 5, {num2:10, num3: 56} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment