assigning values to multiple variable
let [one,two,three,four] = [1,2,3,4]
short circuit evaluation
const a = true
const go=()=>console.log('nice')
if(a){
go();
}
a && go();
swap two variables
[a,b] = [b,a]
multiple condition checking
if(value===1||value===2|| value ===3){
}
if([1,2,3].include(value){
}
object property assignement
let firstName=marwen
let lastName=labidi
const o={
firstName:firstName,
lastName:lastName
}
const o={
firstName,
lastName
}
string into a number
let string =`555`
let number=+s
number into a string
let number=555
let string=number+``
repeating a string for multiple time
const string=`hello`
string.repeat('5');
power
const p=5**2
bitwise operator
const n=5.00034
console.log(~~n)
mergin array
const arr1=[1,2,3,4]
const arr2=[5,6,7,8]
const arrMerg=[...arr1,...arr2]
copying
const arr1=[1,2,3,4]
const copyarr=[...arr1]
const arr =[1,2,3,[3,4],[[55,5544],[4,5]]]
const copyarr=Json.parse(Json.stringify(arr))
use `cloneDeep()` methode in `loadsh` library
run event listener only once
button.addEventListener('click',()=>{},{once:true})
empty array in one line
arr.length=0
shuffle an array
arr.sort(()=>Math.random-0.5)
check if an object is empty
const o={}
Object.entries(o).length===0
filter duplicate values from array
const arr=[3,3,3,3,3]
const result=[... new Set(arr)]
remove falsy values from array
const arr=[false,undefined,4,4,0,null]
arr.filter(()=>Boolean) // [4,4]
convert any value to boolean
!! string
!! 0
!! null
!! 5
resizing array
const arr=[1,3,3,4,4]
arr.length(1)
flatten multidimensional array
const arr=[1,2,3,4,[[[4]]]]
arr.flat(infinity)
replace all occurrence of a string
// use replace.All() methode
log value with variable name
let name=marwen
console.log({marwen})
const t1=performance.now()
const t2=performance.now()
console.log(t1-t2)
check not a number
// use `isNaN()` methode
global this
// `globalThis` replace this and self and windows in the same time
.at()
methode
const arr=[1,2,3,4,5]
arr.at(-2)//4
optional chaining
const o={}
o.address
o?.address
logical multiassignement
let a
a??=44
let b=77
b??=100
logical assignment
let x=0
x||=2
let x=0
x&&=2