This question already has answers here:
Get the description of a ES6 Symbol
(4 answers)
Closed 5 years ago.
I've a String and a Symbol defined in Javascript with same value Hello, Stack Overflow!. How do I compare them for equality? All the comparisons I've tried below return false.
var string="Hello, Stack Overflow!";
var symbol=Symbol("Hello, Stack Overflow!");
console.log(string == symbol);
console.log(string == symbol.toPrimitive);
console.log(string == symbol.toString);
console.log(string == symbol.toStringTag);
console.log(string === symbol);
console.log(string === symbol.toPrimitive);
console.log(string === symbol.toString);
console.log(string === symbol.toStringTag);
You can convert the string to symbol then perform the comparison.
console.log(Symbol(string).toString() == symbol.toString())
Symbol() will always return a unique value
You can use it slightly differently as below
Symbol.keyFor(Symbol.for("Hello, Stack Overflow!"))== "Hello, Stack Overflow!"; // True
Please read more
https://developer.mozilla.org/en-US/docs/Glossary/Symbol
Related
This question already has answers here:
Precedence: Logical or vs. Ternary operator
(2 answers)
Closed 3 years ago.
payload = {type: 3}
const type = payload.type || state.active === "period" ? 1 : 2;
// type returns 1
I'm surprised by the return of type which is 1.. I was expecting it to be 3.. What happened here? What I want to really achieve is if the type index is not available then state.active === "period" ? 1 : 2 will be the basis of the value of type..
How to achieve this in a clean one line?
You need parentheses, because of the operator precedence.
const type = payload.type || (state.active === "period" ? 1 : 2);
This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 8 years ago.
I'm trying to comment out code that I used from a tutorial but haven't actually seen a ?-mark used in JavaScript...
This is a small part of the code below:
this.year = (isNaN(year) || year == null) ? calCurrent.getFullYear() : year;
What you are referring to is the ternary operator which is an inline conditional statement. To illustrate:
this.year = (isNaN(year) || year == null) ? calCurrent.getFullYear() : year;
is equivalent to
if(isNaN(year) || year == null){
this.year=calCurrent.getFullYear()
}
else{
this.year=year;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Is there a difference between !== and != in PHP?
Javascript === vs == : Does it matter which “equal” operator I use?
In some cases when checking for not equal, I saw using != and in some places i saw !==. Is there any difference in that?
Example:
var x = 10;
if (x != 10) {
//...
}
and
if (x !== 10) {
//...
}
== compares only the value and converts between types to find an equality, === compares the types as well.
== means equal
=== means identical
1 is equal to "1", but not identical, because 1 is an integer and "1" is a string.
They are different in terms of strictness of comparison. !== compares variable types in addition to values.
!== will also check the type (int, string, etc.) while != doesn't.
For more information, see the PHP comparison operator documentation.
The !== is strict not equal: Difference between == and === in JavaScript
The difference is that
== (and !=) compare only the value,
=== (and !==) compare the value and the type.
For example
"1" == 1 returns true
"1" === 1 returns false, because one is a string and the other is an integer
Hope this helps. Cheers
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Looking into the answer of Chris Brandsma in Advanced JavaScript Interview Questions what is === in Javascript.
If possible please provide a simple example
=== is the strict equal operator. It only returns a Boolean True if both the operands are equal and of the same type. If a is 2, and b is 4,
a === 2 (True)
b === 4 (True)
a === '2' (False)
vs True for all of the following,
a == 2
a == "2"
2 == '2'
=== is 'strict equal operator'. It returns true if both the operands are equal AND are of same type.
a = 2
b = '2'
a == b //returns True
a === b //returns False
Take a look at this tutorial.
please refer Strict Equality Check..
This question already has answers here:
How do I check for an empty/undefined/null string in JavaScript?
(52 answers)
Closed 3 years ago.
I know this is really basic, but I am new to javascript and can't find an answer anywhere.
How can I check if a string is empty?
I check length.
if (str.length == 0) {
}
If you want to know if it's an empty string use === instead of ==.
if(variable === "") {
}
This is because === will only return true if the values on both sides are of the same type, in this case a string.
for example:
(false == "") will return true, and (false === "") will return false.
This should work:
if (variable === "") {
}
But for a better check:
if(str === null || str === '')
{
//enter code here
}
if (value == "") {
// it is empty
}