-3

I am new to programming and JavaScript so please bear with me if its a stupid question.

I initialised two variables

let firstName = "blah";
let FirstName = "bleh";

When i write a below if statement i expected the output to be "right on" since the variable names are different (case-sensitive), but i get "boink". Could anyone kindly explain whats happening here?

if (firstName = FirstName) {
    console.log('boink')
} else {
    console.log('right on')
}
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
Prashanth
  • 11
  • 2
  • 1
    as has been written, just to elaborate a bit more on why it is as written below. `=` means now `firstName` will take on the value of `FirstName` whereas `===` is an exact comparison (of type and location in memory), meaning it is asking the question "is firstName the same as FirstName or not?" and `==` is an approximate comparison, this would not matter in your case. – Michael May 12 '19 at 15:17
  • @Michael you slightly misunderstand `===` - see [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness) for more details. It does not have anything to do with location in memory. – Mulan May 12 '19 at 15:20
  • sure does, see here: https://stackoverflow.com/questions/24002819/difference-between-and – Michael May 12 '19 at 15:30
  • @Michael: that’s for Swift? this question is about JavaScript. – Erik Kaplun May 12 '19 at 15:42
  • @Erik Allik it't the same in JS – Michael May 12 '19 at 15:45
  • @Michael yeah but isn’t it confusing to answer a JS question with a Swift answer... – Erik Kaplun May 12 '19 at 15:48
  • It had a more clear explanation then the other ones I saw...it's a fact take it or leave it that's how JS works. – Michael May 12 '19 at 15:50
  • @Michael, but you're wrong. `const a = 5, b = 5; a === b // true` even though `a` and `b` do not share the same space in memory. `===` compares type and _value_, not memory location explicitly. In the case of objects, their "value" is a reference and objects with the same reference are considered equal by `===`. – Mulan May 12 '19 at 16:04

1 Answers1

2

Could anyone kindly explain whats happening here

Actually firstName = FirstName is an Assignment expression and it will return the value on the right handside which is "bleh" which is truthy value. So the first block is executed

You are using assignment operator you need to use comparison operator(== or ===)

let firstName = "blah";
let FirstName = "bleh";

if (firstName === FirstName) {
    console.log('boink')
}
else {
    console.log('right on')
}
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • Why does it return *the value on the right handside*? Shouldn't it return like an empty string or undefined? +1 – k3llydev May 12 '19 at 15:29
  • @k3lly.dev Try using `console.log(firstName = FirstName)` in the above snippet – Maheer Ali May 12 '19 at 15:29
  • I know it does, but since variable definitions in console return `undefined`, I was just curious about this behavior. Going to do a bit of research. – k3llydev May 12 '19 at 15:32
  • 1
    @k3lly.dev See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Assignment_2 and read the first line **The assignment operation evaluates to the assigned value** – Maheer Ali May 12 '19 at 15:33
  • Thank you for the answer @MaheerAli – Prashanth May 13 '19 at 21:31
  • @Prashanth You are welcome. Consider accepting the answer if you are satisfied witht it – Maheer Ali May 13 '19 at 22:36