Jump to content

What is the difference between! == and! = in JavaScript?


SubhanUllah

Recommended Posts

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’

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...