Wednesday, August 18, 2021

Equality Operators : == VS === in Javascript Programming

Equality Operators : We Use Equality Operators in Javascript to Compare Value for logic building.

== Operator - Use for compare two value not a Data type. For Example 18 == 18 is same value and 18 == '18'(in String Format) is same.

=== Operator - Check both Value and Data Type. For Example 18 === 18 is same but 18 === '18' (String) not same becase both are diffrent in data type. one is number and second is string. Always use === in your code for comparing two values

let age = '30';
if (age === 30) {
console.log(`This check both value and Data type`);
}
if (age == 30) {
console.log(`This Check Only value not data type`);
}


Using Prompt() to Get Value from User And Compare With Another Number using == and === Operators

const favNumber = prompt('What is Your Faviourite Number ?');
if (favNumber == 20) {
console.log(`This is 20 Your Number`); // Print This Because Check only value not work for ===
}


// to compare data type as true use Number() for convert number not string
const favNumber = Number(prompt('Input Your Fav Number ?'));
if (favNumber === 20) {
console.log(`This Number Checked both value and Data Type`);
}

0 comments

Post a Comment