Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Two JavaScript Operators Explained

DZone's Guide to

Two JavaScript Operators Explained

While JavaScript is a great language for developing sites and web applications, it can be a bit confusing. Today, we take a look at two very similar JS operators.

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

Let's discuss the difference between == and ===.

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

The == operator compares the 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 and 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

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

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

var a =10;   
var b =20;
console.log(a === b )      // output
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 certain ab0ut the type of the variable you are comparing, then you can use ===, otherwise, you should use ==.

However, it is always advised to use === for comparisons.

Question: I know that numbers and strings are two different data types in JavaScript. Is there any other data type available?

Yes, there are seven different data types available in JavaScript, of which 6 types are primitive and the seventh is an 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 an integer or float, everything is considered as a number.

String

Example: "nodesimplified" is a string.

Symbol

A data type whose instance is unique and immutable.


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


This article is originally published from Node Simplified. Take a look into Top Javascript Tips and Tricks.

If you enjoyed this article, please share with your developer friends. Thanks for reading. 

Take a look at the Indigo.Design sample applications to learn more about how apps are created with design to code software.

Topics:
javascript ,web dev ,javascript examples

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}