Javascript Data Types, Operators, and Their Confusion

‘1’ doesn’t equal 1 but [] is equal to ‘ ’ ??

Phyllis
2 min readJul 1, 2020

=> indicates what the expression would evaluate to. Try it out in your console (in Chrome menu: View -> Developer -> Javascript Console)!

3–2 => 1

‘Hello’ + ‘, ‘ + `world!` => “Hello, world!”

‘High ‘ + 5 + ‘!’ => “High 5!”

1 + 2 + 3 + 4 + 5 => 15

‘1' + 2 + 3 + 4 + 5 => ‘12345’

1 + ‘2’ + 3 + 4 + 5 => “12345”

1 + 2 + ‘3’ + 4 + 5 => “3345”

Why? When it reaches a string, it stops adding numbers and starts concatenating
1 + 2 + 3 + ‘4’ + 5 => “645”

1 + 2 + 3 + 4 + ‘5’ => “105”

typeof 42 => “number”

typeof 3.141592653589793 => “number”

typeof 5e-324 => “number”

typeof -Infinity => “number”

typeof ‘I am a string.’ => “string”

typeof ‘Me too!’ => “string”

typeof `Me three!`=> “string”

typeof ‘’ => “string”

typeof true => “boolean”

typeof false => “boolean”

typeof {} => “object”

let dogs = [‘Byron’, ‘Cubby’, ‘Boo Radley’, ‘Luca’];
typeof dogs => “object”

let dogs = [‘Byron’, ‘Cubby’, ‘Boo Radley’, ‘Luca’];
typeof dogs => “object”

typeof undefined => “undefined”

“Undefined” is a misnomer. Instead of ‘not defined,’ it actually means declared but not defined with a value.

let unassignedVariable;
typeof unassignedVariable => “undefined”

unassignedVariable = ‘’;
typeof unassignedVariable => “string”

true == 1 => true
false == 1 => false
false == 0 =>true
null == undefined =>true
‘’ == 0 =>true
’n’ == 1 =>false
‘1’ == 1 =>true
[] == ‘’ =>true

‘88’ > ‘9’ => false

Why? Strings are compared with other strings lexicographically, meaning character-by-character from left-to-right. Make sure that you tell JavaScript to convert the string to a number first, and then compare.

Go to learn.co by the Flatiron School to view this and much more information on programming.

--

--

Phyllis
Phyllis

Written by Phyllis

Product manager | Leading with empathy.

No responses yet