[JavaScript] Difference between == and === in condition check? Javascript datatypes explained in detail.

Lets discuss about the different between == and ===.

Usually == or === is used to check the condition. Many people don't know about the difference between these two comparison operators.

== Operator compares only value of the variables.

var a =10;   
var b =20;
console.log(a == b )      // output : false

------------------------

var x = 5;
console.log(typeof(x));   // output : number
var y ="5";
console.log(typeof(y));   // output : string

console.log(x == y);      // output : true


== Doesn't check the type of variables(x & y). It just compares the value of x and y.

var x = 5;
console.log(typeof(x));   // output : number
var y ="6";
console.log(typeof(y));   // output : string

console.log(x == y);      // output : false

typeof() command is used to fetch the datatype of the variable.


=== Operator compares both value and type of the variables.


var a =10;   
var b =20;
console.log(a === b )      // output : false

------------------------

var x = 5;
console.log(typeof(x));   // output : number
var y ="5";
console.log(typeof(y));   // output : string

console.log(x === y);     // output : false

------------------------


var x = 5;
console.log(typeof(x));   // output : number
var y = 5;
console.log(typeof(y));   // output : number

console.log(x === y);     // output : true

If you are sure with the type of the variable you are comparing then you can use "===" else using "==" will be a good option.



☝☝Question: Now I know number and string are two different data types available in javascript? is there any other data type available?
Yes, There are seven different data types available in the javascript, in which 6 types are primitive and seventh one is the object.
  • Six Primitive data types:
    • Boolean. 
    • null.
    • undefined.
    • Number.
    • String.
    • Symbol. (Added in ECMAScript 2015).
  • Object.

Boolean    - Either true or false.
null           - it is data type in javascript. it is not same as Null or NULL. Javascript is case sensitive.
undefined - is also a data type in javascript.
Number    - It can be integer or float, everything is considered as the number.
String       - example: nodesimplified is a string.
Symbol    - data type whose instance is unique and immutable.

So typeof() will return any of the above seven value.

Note: typeof(null) will return object.



Comments

  1. typeof never returns "null"; typeof null === "object".

    Also, typeof is an operator, not a function, and does not need parentheses.

    How do you explain: console.log({} == {}) // output: false? Same value, same type, but not equal?

    ReplyDelete
    Replies
    1. Agreed.

      typeof(null) //"object"
      typeof(4) //"number"
      typeof("raghu") //"string"

      Even we can use typeof as a function and it will work.

      check this URL : http://www.nodesimplified.com/2017/08/javascript-some-weird-facts-about.html

      Delete

Post a Comment