Java Scripts Notes
Alt + up/down shortcut to move code line
Shift+ alt+ down duplicate row
1. Use || operator to default values
Var color=’red’;
Let selectedColor = color ||’red’;
2. Template literal
Use template literal (tick operator) to define string across new line
` my name
Is
${person.name}.
`
3. Rest Operator
The rest operator is used to pass varying number of parameters to function.
4. Use Getter and setter function
Instead of creating function to read and write object property, define getter/setter to read it like
object property.
5. Function passed values validation
Use typeof to validate arguments value
Set fulName(value)
{
If(typeof value !== ‘string’)
throw new Error(‘not valid input’)
--Function logic goes here--
}
6. Hoisting
The hoisting is the process of moving function declaration to top of file and this is done
automatically by the JS engine before executing code
7. Let vs var
Var => function – scoped variable
(ES6 specification )Let, const => block scoped variable
8. This
This represent the object that is executing the current function
Function is part of object (Method) object
Function --? Global (windows, global)
When you call function using New keyword, JS creates empty object and use this to point this
new object
9. Array function
Search primitive data type: - indexOf, lastIndexOf, includes
remove elements of array :-pop, shift, splice
Search object type:- find, findIndex
Empty array:- set .length=0, splice(0,number.length)
Combine array: concat, spread operator( […first,’a’ ,…second])
Iterate array: forEach(function(){} )
10. Arrow function
courses.find( function(courses){
return courses.name === 'c';
})
let aa = courses.find( courses =>{
return courses.name === 'c';
})
courses.find( courses => courses.name === 'c')
11. Arguments
12. Factory/constructor Function (Pattern to create object)
Three things happen when new operator is being used.
1. Created empty JS object const x ={}
2. Set this to point this object
3. Return new object
Use Pascal naming to name construction function
13. IIFE
14.
15. Add object property
16.
Define property