ReactJS

    The difference between the nullish coalescing (??) operator and the logical OR (||) operator in JavaScript.


    Nullish Coalescing Operator

    The syntax obtains two question marks which is ‘??’. This operator extracts multiple variables with the first approach fetching the first operand which is not equal to ‘null’ or ‘undefined’. 

    OR Operator

    On the other hand ‘||’ OR operator retrieves all operands starting from the left and and stops at the first one that is ‘truthy’. Anything that is not considered ‘falsy’ can be tagged as a truthy operand. Examples of falsy operands include ‘0’, ‘false’, ‘null’, ‘undefined’, ‘NaN’ or even an `empty` string.

    Key difference

    • The first operator is known to ‘??’ eliminate lower operands such as ‘null’ or ‘undefined values’.
    • In contrast, the second operator only excludes the definition of ‘truthy’ operands from the operands.
    false ?? "value"// falseundefined ?? "value"// "value"null ?? "value"// "value"NaN ?? "value"// NaN/** compared to || **/false || "value"// "value"undefined || "value"// "value"null || "value"// "value"NaN || "value"// "value"

     

    Ready to transform your business with our technology solutions? Contact Us today to Leverage Our ReactJS Expertise.

    Share

    facebook
    LinkedIn
    Twitter
    Mail
    React

    Related Center Of Excellence