JavaScript ‘===’ vs ‘==’Comparison Operator
Last Updated :
09 Dec, 2022
In Javascript(ES6), there are four ways to test equality which are listed below:
- Using '==' operator
- Using '===' operator
- SameValueZero: used mainly in sets, maps and arrays.
- SameValue: used elsewhere
Now, our main concern is getting to know the difference between the '==' and '===' operators that the javascript provides, though they look similar, they are very different.
- JavaScript '==' operator: In Javascript, the '==' operator is also known as the loose equality operator which is mainly used to compare two values on both sides and then return true or false. This operator checks equality only after converting both the values to a common type i.e type coercion.
Note: Type coercion means that the two values are compared only after attempting to convert them into the same type. Let's look at all the values in which the '==' operator will return true.
Boolean values of literals during comparison:
True
- '0'
- 'false' // false wrapped in string.
- []
- {}
- function(){}
False
- '' or "" // empty string
- false
- 0
- null
- undefined
- NaN // not-a-number
Example 1:
javascript
<script>
// '==' operator
console.log(21 == 21);
console.log(21 == '21');
console.log('food is love'=='food is love');
console.log(true == 1);
console.log(false == 0);
console.log(null == undefined);
</script>
Output: In the above code, when we compare 21 with '21' the javascript will convert the '21' into the number value of 21, and hence we get true, a similar thing happens when we try to check if 'true == 1', in that case, the javascript basically converts 1 into a truthy value and then compares and return the boolean true.
The case of (null == undefined) is special as when we compare these values we get true. Both null and undefined are false values and they represent an 'empty' value or undefined in js, hence the comparison with '==' operator returns true.
true
true
true
true
true
true
Now, let's look at all the values in which the '==' operator will return false.
Example 2: In this code when we compare null with false we get false, as null being a primitive data type it can never be equal to a boolean value, even though they belong to the same false group.
javascript
<script>
// '==' operator
console.log(21 == 32);
console.log(21 == '32');
console.log(true == 0);
console.log(null == false);
</script>
Output:
false
false
false
false
JavaScript '===' operator: Also known as strict equality operator, it compares both the value and the type which is why the name "strict equality".
Example 1: Let's see some code where '===' operator will return true.
javascript
</script>
// '===' operator
console.log('hello world' === 'hello world');
console.log(true === true);
console.log(5 === 5);
</script>
Output:
true
true
true
Example 2: Simply check the types and values at both sides and then just print out the boolean true or false. Some examples where it will return false.
javascript
<script>
// '===' operator
console.log(true === 1);
console.log(true === 'true');
console.log(5 === '5');
</script>
Output:
false
false
false
Please go through the Difference between double equal vs triple equal JavaScript article to compare '===' and '==' operators.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at
7 min read
JSON Web Token (JWT) A JSON Web Token (JWT) is a standard used to securely transmit information between a client (like a frontend application) and a server (the backend). It is commonly used to verify users' identities, authenticate them, and ensure safe communication between the two. JWTs are mainly used in web apps an
7 min read
Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie
15+ min read
JavaScript Coding Questions and Answers JavaScript is the most commonly used interpreted, and scripted Programming language. It is used to make web pages, mobile applications, web servers, and other platforms. Developed in 1995 by Brendan Eich. Developers should have a solid command over this because many job roles need proficiency in Jav
15+ min read
Top 95+ Javascript Projects For 2025 JavaScript is a lightweight, cross-platform programming language that powers dynamic and interactive web content. From real-time updates to interactive maps and animations, JavaScript brings web pages to life.Here, we provided 95+ JavaScript projects with source code and ideas to provide hands-on ex
4 min read
Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9)); // output: 15Function Syntax
5 min read
JavaScript Exercises, Practice Questions and Solutions JavaScript Exercise covers interactive quizzes, tracks progress, and enhances coding skills with our engaging portal. Ideal for beginners and experienced developers, Level up your JavaScript proficiency at your own pace. Start coding now! A step-by-step JavaScript practice guide for beginner to adva
3 min read
HTML DOM (Document Object Model) The HTML DOM (Document Object Model) is a programming interface that represents the structure of a web page in a way that programming languages like JavaScript can understand and manipulate. Think of it as a tree of objects where each part of your HTML document (elements, attributes, text) is repres
6 min read