Jump to content

Recommended Posts

Posted
When you use “=” you are assigning the value of something like for example a variable to something else.

For example.

let var1 = 'hello' 
 
var1 = 'world' 
 
console.log(var1) 
//outputs 'world', because we reassigned the value from 'hello' to 'world' 
Now == is used to check if two values are the same, but it does not check type, meaning that.

‘1’ is a string, because it’s wrapped in quotes.

1 is a number because it’s an integer and it is not wrapped in quotes.

if we do for example.

‘1’ == 1 
//output would be 'true' because the == operator is checking to see if '1' and 1, have the same value which is the number 1, it is not checking if '1' is a number, or if 1 is a string it's checking if '1' and 1 have the same value which is the number 1. 
To check for type we use the strict equality operator or ===

So in the previous example, == checks for value not for type, === checks for value AND type.

Which means that

‘1’ === 1

would return “false” because ‘1’ is not a number it’s a string, and 1 is a number not a string.

1 === 1 returns ‘true’

‘1’ === ‘1’ returns ‘true’

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...