Open In App

Difference between double equal vs triple equal JavaScript

Last Updated : 06 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Double equal: The double equal(‘==’) operator tests for abstract equality i.e. it does the necessary type conversions before doing the equality comparison.

Triple equal: The triple equal(‘===’) operator tests for strict equality i.e it will not do the type conversion hence if the two values are not of the same type, when compared, it will return false. 

Example 1: In this example, we will check abstract and strict quality. One will return true between a string 9 and number 9. Because there is no type comparison, in the case of ‘===’ it will return false. 

Javascript




<script>
    // In R.H.S. string "9" is converted into
    // number 9, hence returns true.
    console.log(9 == "9"); 
       
    // Here no type of conversion takes place,
    // hence returns false
    console.log(9 === "9"); 
</script>


Output:

true
false

We have an article on JavaScript ‘===’ vs ‘==’ Comparison Operator, you can go through that article for in-depth information.

Example 2: Here L.H.S. is a string literal whereas R.H.S. is a string object, due to the type conversion of a string object into a string literal, it returns true. 

Javascript




<script>
    // type conversion takes place
    console.log("GeeksforGeeks" == new String("GeeksforGeeks"));
      
    // No type of conversion takes place
    console.log("GeeksforGeeks" === new String("GeeksforGeeks"));
</script>


Output:

true
false

Example 3: Here number 1 is converted into true(boolean type) in javascript true is referred to as 1 and false is referred to as 0, hence it returns true. 

Javascript




<script>
    // type conversion
    console.log(true == '1');
      
    // No type conversion so it returns false    
    console.log(true === '1');
</script>


Output:

true
false

In general “===” operator is recommended since it never does type conversion we are doing an exact comparison thus it always produces correct results.



Similar Reads

How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Equal Operator == The comparison operator called Equal Operator is the double equal sign "==". This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and return a false value if both of the values are not same. This should always be kept in mind that the
2 min read
What are these triple dots (...) in JavaScript ?
In JavaScript, there are multiple ways in which we can assign the value of one object to another. Sometimes we do not know the exact number of arguments that are to be assigned. In this case, the triple dots are used.  The triple dots are known as the spread operator, which takes an iterable(array, string, or object) and expands the iterable to ind
4 min read
What are triple-slash Directives in TypeScript ?
Triple-slash directives in TypeScript are special comments that are used to provide instructions to the TypeScript compiler (tsc) or the IDE (Integrated Development Environment) about how to process a TypeScript file. These directives start with three forward slashes (///) and are typically placed at the top of a TypeScript file. Triple-slash direc
6 min read
Difference between single-quoted and double-quoted strings in JavaScript
In this article, we will see the difference between single-quoted and double-quoted strings in JavaScript. Both single-quotes and double-quotes strings in JavaScript are used for creating string literals. But the basic difference between them comes into play when the character which needed to be escaped is itself a single-quoted or double-quoted st
5 min read
What is the difference between single-quoted and double-quoted strings in PHP?
Single or double quotes in PHP programming are used to define a string. But, there are lots of differences between these two. Single-quoted Strings: It is the easiest way to define a string. You can use it when you want the string to be exactly as it is written. All the escape sequences like \r or \n, will be output as specified instead of having a
3 min read
Difference between Double and Single Curly Brace in AngularJS ?
In the AngualrJS framework, we can build attractive and dynamic web applications through different utilities. In AngularJS, there are double curly braces "{{ }}" and also single curly braces "{ }". The Double Curly Braces in AngularJS are mostly used for Data Binding, which also means that we are allowed to insert dynamic content into our HTML temp
4 min read
What is (~~) "double tilde" operator in JavaScript ?
This is a special kind of operator in JavaScript. To understand the double tilde operator, first, we need to discuss the tilde operator or Bitwise NOT. The (~) tilde operator takes any number and inverts the binary digits, for example, if the number is (100111) after inversion it would be (011000). So if we think closely it can be noticed that afte
3 min read
When should we use double or single quotes in JavaScript ?
This article aims to clarify when you should use double or single quotes while coding in JavaScript. If you have worked with JavaScript, you may know that it allows expressing string literals with both double quotes (“) and single quotes (‘). There are many differences between double and single quotes in other programming languages. In JavaScript,
3 min read
Explain the 'double negative' Trick in JavaScript
In programming, a "double negative" is a technique used to convert a value to its corresponding boolean value. This technique involves using two negation operators (!) to negate a value twice, resulting in a boolean representation of the original value. The double negative technique is often used to convert truthy and falsy values to their correspo
2 min read
What is the use of Single Quotes ('') & Double Quotes ("") for Strings in JavaScript ?
In JavaScript, both single quotes ('') and double quotes ("") can be used to create strings, and they are interchangeable in most situations. However, there are a couple of differences worth noting: Quoting Inside Strings: If your string contains an apostrophe (single quote) and you use double quotes to define the string, you won't need to escape t
1 min read