Skip to main content

Double (==) equal and Triple (===) equal sign - how it works on JavaScript | a brief explanation!

 In JavaScript, we may come across double equal (==) and triple equal (===) sign while checking the equality of two variables' values.

But why two different operators are there for the same operation of equality check? What do they actually mean? You will find the answers in this blog.



1. How == (double equal) works

Double equal does the loose equality check. It checks the value equality only. It converts the type of the variables to match each other. Some common examples of double equal (==) check of two variables x and y are given below in tabular form -

x
y
x==y
undefined
undefined / null
true
undefined
true / false / NaN / 0 / 17 / 'string'
false
null
true / false / NaN / 0 / 12 / 'string'
false
null
null / undefined
true
NaN
any-value
false
new String("mukitul")
"mukitul"
true
{property:'value'}
{property:'value'}
false

2. How === (triple equal) works
Triple equal does the strict equality check. It checks the equality of the value and the variable's type. It doesn't forcefully convert the type of variables. Some common examples of double equal (===) check of two variables x and y are given below in tabular form -
x
y
x===y
undefined
undefined
true
undefined
null / true / false / NaN / 0 / 17 / 'string'
false
null
true / false / NaN / 0 / 12 / 'string'
false
null
null
true
NaN
any-value
false
new String("mukitul")
"mukitul"
false
{property:'value'}
{property:'value'}
false

3. When to use == or ===
Triple equal (===) operator ensures two operands (or variables) are of the same value and type when it returns true or returns false. Double equal (==) doesn't check type equality, it only considers the value of two operands.
Suppose a scenario where the response from an API call gives both "true" and true, in this case, we can check the response in JavaScript/client-side using double equal (==). But we should keep in mind that, it is highly recommended to use triple equal (===) when checking the equality of two operands unless there is any extreme situation occurs like the above scenario.

So, if we don't have any strong use case for using double equal (==) then we must use triple equal (===) which will save us from many potential bugs.

Have a good day!

Comments

Popular posts from this blog

কিভাবে উইন্ডোজ ১০ এ Java/JDK ৮ ইন্সটল করবেন? JDK 8 installation guide!

এই ব্লগে আমরা দেখবো কিভাবে একটি উইন্ডোজ ১০ পিসি তে Java/JDK ইন্সটল করতে হয়। Java ইন্সটল করা বলতে আমরা বুঝবো JDK বা Java Development Kit ইন্সটল করাকে। আমরা এই ব্লগে জানবো কি করে Oracle JDK এর ভার্সন ৮ ইন্সটল করতে হয়। ধাপ ১ঃ ডাউনলোড JDK ৮ ডাউনলোড লিঙ্কঃ  https://www.oracle.com/java/technologies/downloads/#java8 অথবা  https://www.oracle.com/java/technologies/downloads/  এই লিঙ্ক এ গিয়েও স্ক্রল করে নিচে নেমে Java 8 choose করতে পারেন। Available product/file গুলো থেকে x64 Installer এর .exe file টা ডাউনলোড করি (তবে আপনার পিসি যদি 32-bit অপারেটিং সিস্টেম এর হয়, সেক্ষেত্রে আপনি ডাউনলোড করবেন x86 Installer এর exe file টা-কে)। ডাউনলোড করার জন্য Oracle এ অ্যাকাউন্ট থাকতে হয়। আপনার যদি অ্যাকাউন্ট না থাকে তবে অ্যাকাউন্ট create করে নিবেন। Oracle এ অ্যাকাউন্ট create করা একদম free of cost. ধাপ ২ঃ ইন্সটল JDK ৮ ডাউনলোড করা হয়ে গেলে .exe টাতে double click করে ইন্সটল করা শুরু করি। নিচের ছবি গুলোর মতো একটা করা উইন্ডো আপনার সামনে ওপেন হবে এবং আপনি Next button ক্লিক করে সামনে এগিয়ে যা...

How to create PuTTY shortcut in Windows to establish connection to Linux server? | Step by step guide is here!

PuTTY is open-source software and an SSH and telnet client for Windows. PuTTY helps to establish an SSH connection from a Windows machine to a Linux server. However it is not limited to this feature only, it has loads of other features.  This article will guide us in creating a PutTTY shortcut for windows. This is a 7-step procedure and step 6 is very important! Step 1: Download  the MSI file and install PuTTY on your Windows machine. Step 2: Go to your PuTTY folder. It is usually located in  C:\Program Files\PuTTY  or  C:\Program Files(x86)\PuTTY Fig: image_1 Step 3: Create a shortcut of putty.exe - the shortcut will be created in the desktop folder. Fig: image_2 Step 4: Navigate to your desktop folder and you will find the shortcut named  putty.exe - Shortcut Fig: image_3 Step 5: Right-click on the  putty.exe - Shortcut and then click properties - properties window will open, like the images shown below (image_4 and image_5)-    Fig...

JDK 17 installation guide | কিভাবে উইন্ডোজ ১০ এ Java/JDK ১৭ ইন্সটল করবেন? How to install JDK 17 on windows 10?

এই ব্লগে আমরা জানবো কিভাবে উইন্ডোজ ১০ পিসি তে Java/JDK ইন্সটল করতে হয়। Java ইন্সটল করা বলতে আমরা বুঝবো JDK বা Java Development Kit ইন্সটল করাকে। আমরা এই ব্লগে Oracle JDK এর ভার্সন ১৭ কিভাবে ইন্সটল করতে হয় সেই ধাপগুলো জানবো।  ধাপ ১ঃ ডাউনলোড JDK ১৭ ডাউনলোড লিঙ্কঃ  https://www.oracle.com/java/technologies/downloads/#java17 অথবা  https://www.oracle.com/java/technologies/downloads/  এই লিঙ্ক এ গিয়েও স্ক্রল করে নিচে নেমে Java 17 choose করতে পারেন। Available product/file গুলো থেকে x64 Installer এর .exe file টা ডাউনলোড করি। ডাউনলোড করার জন্য Oracle এ অ্যাকাউন্ট থাকতে হয়। আপনার যদি অ্যাকাউন্ট না থাকে তবে অ্যাকাউন্ট create করে নিবেন। Oracle এ অ্যাকাউন্ট create করা একদম free of cost. ধাপ ২ঃ ইন্সটল JDK ১৭ ডাউনলোড করা হয়ে গেলে .exe টাতে double click করে ইন্সটল করা শুরু করি। নিচের ছবি গুলোর মতো একটা করা উইন্ডো আপনার সামনে ওপেন হবে এবং আপনি Next button ক্লিক করে সামনে এগিয়ে যাবেন। সবশেষে Close button ক্লিক করে ইন্সটল করা সম্পন্ন করবেন। ধাপ ৩ঃ Environment Variable সেটআপ S...