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
}