JavaScript operator

operator

Often used in JavaScript (excluding triangulation operators)


  • Two punctuation operators (Nullish Coalescing Operator)

    The two punctuation operators return the value of the right term only if the value of the left term is null or undefined; otherwise, they return the value of the left term. // // 0은 포함되지 않음

    const a = null;
    const b = undefined;
    const c = 0;
    const d = '';
    const e = false;
    
    console.log(a ?? 'default'); // 'default'
    console.log(b ?? 'default'); // 'default'
    console.log(c ?? 'default'); // 0 // 0 not included
    console.log(d ?? 'default'); // ''
    console.log(e? 'default'); // false


  • When looking for true values from the left, you can use the ||(logical OR) operator to find true values from the left.

    To find true values from the left, connect variables successively with the | operator, and proceed from left to right until you get the desired value. Returns the first value evaluated as true.

    var value1 = false;
    var value2 = "hey";
    var value3 = true;
    var value4 = 'Hello';
    var value5 = 0;
    
    var result = value1 || value2 || value3 || value4 || value5;// value2 is assigned "hey" When looking for false values from the left of 

  • Use the && operator to find false values from the left.

    To find false values from the left, connect variables successively with the && operator, and proceed from left to right until you get the desired value. Returns the first value evaluated as false.

    var value1 = true;
    var value2 = "hey";
    var value3 = "";
    var value4 = 'Hello';
    var value5 = 0;
    
    var result = value1 && value2 && value3 && value4 && value5; // value3의 값 ""이 할당됨


  • 물결 두 개 연산자 (Double Tilde)

    The two wave operators are operators that leave a given number with no more than a decimal point and leave only an integer part.

    let numA = 5.5;
    console.log(~~numA); // 5
    
    let numB = -10 / 3;
    console.log(~~numB); // -3


  • 참고

    자바스크립트 연산자 참고

    Sep 20, 2023 Views 398