“différence entre =, == et === dans JavaScript” Réponses codées

javascript == vs ===

/**
* In js :
* == it convert the variable values to the same data type before performing comparison.
* example : comparison between string and interger data type
*/
(5 == "5") // returns true

// === it does not convert data type of variable before performing comparison.
// example:
(5 === "5") // return false
Adam Al-Rahman

différence entre =, == et === dans JavaScript

/*
= operator in javascript is called as assign operator. It is used to assign
value to a variable, it is used as: */
variable = "value"
/*      ^^^
here, as you can see, it is saying that variable should be assigned(=) the value
"value" */

/*
== operator in javascript is called the equals operator. It is used to compare
equals between two values, ignoring their types(number, string, boolean, etc.),
it is used as: */
   (5 == "5") // true
/* ^^^^^^^^^^
here, as you can see, it is asking that 5 is equal to "5", ignoring the type, it
returns true as their value is 5 (Note that "5" is converted to 5 for checking
for == comparator) */

/*
=== operator in javascript is called the strict equals operator. It is used to
compare euals between two values, considering their types(number, string,
boolean, stc.), it is used as: */
   (5 === "5") // false
/* ^^^^^^^^^^^
here, as you can see, it is asing that 5 is equal to "5", considering the type,
it returns false as their values is same, but their type is not (Note that "5"
is not converted into 5 for checking for === comparator) */
Pleasant Panther

différence entre || et ?? en js

// The main difference is that nullish coalescing(??) operator will only
// give the result as the right operand only if the left operand is either null or undefined.

// Whereas the OR(||) operator will give the result as right operand 
// for all the falsy values of the left operand.

const a = 0;
// a || 10 --> Will result in 10, as || operator considers 0 as falsy value and resulting the right side operand
console.log(`a || 10 = ${a || 10}`);
// a ?? 10 --> Will result in 0, as ?? operator considers 0 as truthy value and resulting the left side operand
console.log(`a ?? 10 = ${a ?? 10}`);
Anxious Alligator

Réponses similaires à “différence entre =, == et === dans JavaScript”

Questions similaires à “différence entre =, == et === dans JavaScript”

Plus de réponses similaires à “différence entre =, == et === dans JavaScript” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code