Are these lines of JavaScript code equivalent?

Haradzieniec

I've found this string in JavaScript code.

var c = (a.b !== null) ? a.b : null;

This is a shorthand of an if-else statement, however the value null is assigned if it is null. Isn't that ALWAYS equivalent to

var c = a.b

including all cases - exceptions, null, undefined, etc?

In another words, are these lines (always) equivalent?

var c = (a.b !== null) ? a.b : null;

-vs-

var c = a.b
potatopeelings

No, they AREN'T NECESSARILY EQUAL always if b is a getter that updates a variable. It's bad practice to code this way though

var log = 0;
var a = {
    get b() {
        log++;
        return log;
    }
}

var c = (a.b !== null) ? a.b : null;
// outputs 2
console.log(c);
var log = 0;
var a = {
    get b() {
        log++;
        return log;
    }
}

var c = a.b;
// outputs 1
console.log(c);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert jQuery to JavaScript code equivalent

From Dev

What's the equivalent of this code in pure/native javascript?

From Dev

native javascript to jQuery equivalent simple code

From Dev

How to write progress code equivalent to encodeuricomponent of javascript?

From Dev

What's the equivalent of this code in pure/native javascript?

From Dev

What is the PHP equivalent of this cookie code in jQuery/JavaScript?

From Dev

JavaScript's equivalent code for Ruby's range

From Dev

Javascript/NodeJS equivalent code for the Java code Cipher.doFinal(byte[])?

From Dev

Breaking a string into multiple lines inside a javascript code

From Dev

javascript code skips some lines from executing

From Dev

Javascript equivalent to $.on

From Dev

Javascript += equivalent?

From Dev

Why would these three lines of code result in the definition of a Javascript "class"?

From Dev

Does Javascript run lines of code in order? and finish processing then move on?

From Dev

Is it possible to reduce lines of the code from sort helper functions in JavaScript?

From Dev

Converting a JavaScript code using callbacks and RegEx method to equivalent code in C#

From Dev

Haskell equivalent of this Python code

From Dev

C# Equivalent to this code

From Dev

Matlab equivalent code for file

From Dev

%timeit equivalent in code

From Dev

Equivalent code in IronPython

From Dev

C# Equivalent to this code

From Dev

Matlab equivalent code for file

From Dev

sed equivalent code for AIX

From Dev

Equivalent code without the library

From Dev

Is this Swift code equivalent?

From Dev

Is this Haskell code equivalent to this Python code?

From Dev

Is this Haskell code equivalent to this Python code?

From Dev

Equivalent Scala code for Java code

Related Related

  1. 1

    Convert jQuery to JavaScript code equivalent

  2. 2

    What's the equivalent of this code in pure/native javascript?

  3. 3

    native javascript to jQuery equivalent simple code

  4. 4

    How to write progress code equivalent to encodeuricomponent of javascript?

  5. 5

    What's the equivalent of this code in pure/native javascript?

  6. 6

    What is the PHP equivalent of this cookie code in jQuery/JavaScript?

  7. 7

    JavaScript's equivalent code for Ruby's range

  8. 8

    Javascript/NodeJS equivalent code for the Java code Cipher.doFinal(byte[])?

  9. 9

    Breaking a string into multiple lines inside a javascript code

  10. 10

    javascript code skips some lines from executing

  11. 11

    Javascript equivalent to $.on

  12. 12

    Javascript += equivalent?

  13. 13

    Why would these three lines of code result in the definition of a Javascript "class"?

  14. 14

    Does Javascript run lines of code in order? and finish processing then move on?

  15. 15

    Is it possible to reduce lines of the code from sort helper functions in JavaScript?

  16. 16

    Converting a JavaScript code using callbacks and RegEx method to equivalent code in C#

  17. 17

    Haskell equivalent of this Python code

  18. 18

    C# Equivalent to this code

  19. 19

    Matlab equivalent code for file

  20. 20

    %timeit equivalent in code

  21. 21

    Equivalent code in IronPython

  22. 22

    C# Equivalent to this code

  23. 23

    Matlab equivalent code for file

  24. 24

    sed equivalent code for AIX

  25. 25

    Equivalent code without the library

  26. 26

    Is this Swift code equivalent?

  27. 27

    Is this Haskell code equivalent to this Python code?

  28. 28

    Is this Haskell code equivalent to this Python code?

  29. 29

    Equivalent Scala code for Java code

HotTag

Archive