Javascript Short Hands - hochan222/Everything-in-JavaScript GitHub Wiki

The Nullish Coalescing Operator

?? μ—°μ‚°μžλŠ” null κ³Ό undefined 인지 ν™•μΈν•˜μ—¬ ||와 λΉ„μŠ·ν•œ λ™μž‘μ„ ν•œλ‹€.

function myFn(variable1, variable2) {
  let var2 = variable2 ?? "default value"
  return variable1 + var2
}

myFn("this has", " no default value") //returns "this has no default value"
myFn("this has no") //returns "this has no default value"
myFn("this has no", 0) //returns "this has no 0"
function myFn(variable1, variable2) {
  variable2 ??= "default value"
  return variable1 + variable2
}

myFn("this has", " no default value") //returns "this has no default value"
myFn("this has no") //returns "this has no default value"
myFn("this has no", 0) //returns "this has no 0"

The double bitwise NOT operator

같은 값에 두 번 μ‚¬μš©ν•˜λ©΄ Math.floor 와 같은 κ²°κ³Όλ₯Ό μ–»μŠ΅λ‹ˆλ‹€.

let  x  =  3.8
let  y  = ~ x  // 이것은 xλ₯Ό-(3 + 1)둜 λ°”κΏ‰λ‹ˆλ‹€. κΈ°μ–΅ν•˜μ„Έμš”, μˆ«μžλŠ” μ •μˆ˜λ‘œ λ°”λ€λ‹ˆλ‹€
let  z  = ~ y  // 이것은 y (-4)λ₯Ό-(-4 + 1) (3)둜 λ°”κΏ‰λ‹ˆλ‹€.

// λ”°λΌμ„œ λ‹€μŒμ„ μˆ˜ν–‰ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

let  flooredX  = ~~ x  // μœ„μ˜ 두 단계λ₯Ό λ™μ‹œμ— μˆ˜ν–‰ν•©λ‹ˆλ‹€.

Object property assignment

let name:string = "Fernando";
let age:number = 36;
let id:number = 1;

type User = {
  name: string,
  age: number,
  id: number
}

//Old way
let myUser: User = {
  name: name,
  age: age,
  id: id
}

//new way
let myNewUser: User = {
  name,
  age,
  id
}