David
David

Reputation: 323

Equality Comparison assignment

I'm trying to understand how this line of code works. Apparently, isReady will return true if the value for LAUNCH is 'ready' and false if not. What is happening here?

const LAUNCH = 'ready'

const isReady = LAUNCH === 'ready'

Upvotes: 1

Views: 44

Answers (2)

Adelin
Adelin

Reputation: 8209

To add to the correct answer, the order of evaluation makes all the difference in the world.

The following snippet of code aims to help you better understand how things are evaluated in JS:

var obj = {
    true: "stuff"
}

var stuff = function () {
    return function () { 
        return 2+2
    }
}

window[obj["hi".length == 2]]()() == 4 // this weird statement returns true

What happens here is this order of evaluation:

  1. "hi".length is evaluated to 2
  2. 2 == 2 is evaluated to true
  3. obj[true], after true is coerced to a string, is evaluated to "stuff"
  4. window["stuff"] is evaluated to an existent function
  5. Calling that function returns another function
  6. Calling this other function executes 2+2 and returns 4
  7. And, lastly, 4 == 4 is evaluated to true

Upvotes: 2

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48367

What is happening here?

Firstly, it's evaluating the right expression LAUNCH === 'ready' which is true.

Then, just assign this value to isReady variable. isReady variable will hold everytime one boolean value.

Upvotes: 3

Related Questions