Working of "===" operator - javascript

In JavaScript Programming language, I have seen the condition:
if(data=== 'y')
I can't understand the operator ===. Someone please explain the purpose of this operator.

The operator === is used to determine that whether both the value and the data types are same or not whereas the operator == only checks for the value. See an example here

It means that the data types have to be same, as well as the values, for the expression to be true.
By the way, Java does not have this operator. Do you mean Javascript?

Related

jQuery/Javascript the comparison operators "===" and "=="

in dreamweaver cc 2015, when i am using the comparison operators in jquery/javascript programming like:
if(x == "")
DW shows an error that Expected === and instead saw ==. My question is that what is the difference between === and == ?.
As i know in other languages like C# etc the === operator means that the comparison will check the Data Type of the value as well as the value. In javascript or jquery is there any problem if i use the == instead of === ? or still the result will be the same in jquery / javascript?
In Javascript, === do not try to coerce the type of the variables you are testing, while == will do its best to 'typecast' those variables if needed to compare them.
For instance 1 == '1' returns true, while 1 === '1' returns false since you are comparing a number to a string.
Lastly, jQuery and pure javascript both uses the same === and == operators. Hence, there will not be any difference between the two.
The MDN documentation is pretty good too.
The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. Both are equally quick.
For more you can check this answer in stack oveflow
Which equals operator (== vs ===) should be used in JavaScript comparisons?
There is a slight difference between == and === operators. If you are using == that means you are comparing just values for example (5=='5') will return you true whereas first operand is integer and the second operand is string.Now considering the same example with === i.e (5==='5') will return you false because the '===' operator will check the value as well as type conversion and in this case integer cannot be equal to the string. Hope this helps you.

If I use two negation operators on a string am I using type coercion?

Consider the following code:
!!('foo');
The negation operator uses the abstract operation ToBoolean to perform a type conversion, but my question is - does this involve type coercion?
According to wikipedia http://en.wikipedia.org/wiki/Type_conversion " the word coercion is used to denote an implicit conversion", so yes there is type coercion involved, since the conversion is enforced.
Alo take a look at the answer to this question What is the difference between casting and coercing?
Considering that coercion is simply an implicit conversion, that being either a cast or any kind of processed conversion, than yes, this involves coercion, because you didn't convert it explicitly.
!!(x) appears to return the same result as Boolean(x). You can see this for yourself by typing the following into the JavaScript console:
Boolean(false) === !!(false)
Boolean(0) === !!(0)
Boolean("") === !!("")
Boolean(null) === !!(null)
Boolean(undefined) === !!(undefined)
Boolean(NaN) === !!(NaN)
All over values are 'truthy' in JavaScript. Haven't checked every other available value in JavaScript; that could take some time. ;-)

What kind of if statement is this? [duplicate]

This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 8 years ago.
So I used an online JavaScript optimizer and it did something strange to my IF statements that is new to me.
{1===a?window.addEventListener("scroll",turnOff):0===a&&window.removeEventListener("scroll",turnoff)}
What in the world is this? Help me to understand the parts of it please.
The three equal signs are a representation of strict comparison operators. Unlike the usual double equal signs, using === compares both the type and value of the expressions being compared. Both the type and value must be equal for the statement to evaluate to true.
The ? and : are shorthand versions of the if statement. When 1===a is true, the statement after the question mark is evaluated. Otherwise, the statement after the colon is evaluated. This shorthand version of the if statement is known as a Conditional (or Ternary) operator.
It is a Conditional (Ternary) operator.
You can read more about ternary operators at the developer Mozilla docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
It is structured like this:
condition ? code if true : code if false
The ? Operator is used just as an if.
if(a){
b;
} else {
c;
}
is equal to
a ? b : c;
It's called a ternary operator. It compares the condition
if a === 1
and returns the first result if the condition is true:
window.addEventListener("scroll",turnOff)
and the second if false:
0===a&&window.removeEventListener("scroll",turnoff)
This line of code does two things, sets a equal to 0 and removes an event listener.
See this tutorial

Unexpected result from expression using &

Here if
document.write(eval("(2 == 2)"));
it prints true
And
document.write(eval("(2 == 2)&(5<10)"));
it prints 1
Why not it always returns true or false.If conditions increases in string it gives 0 or 1.What is the way to get same type of result.
Because & isn't a logical operator, it's a bitwise (numeric) operator. You probably meant &&, the logical AND operator.
Note that there's no reason for using eval in your code examples. (There's almost never any reason to use eval, there's almost always a better alternative — in this case, you don't even need an alternative.) Removing it would have exactly the same result.
It prints 1 because you used the bitwise and operator (&) instead of the logical and operator (&&).
And because JavaScript is loosely typed it will treat the boolean value true as an int value of 1, which you can check by invoking
document.write(eval("(2 == 2)+(5<10)"));
the result will be
2
& is a bitwise operator and returns a number. Try "(2 == 2)&&(5<10)"

What Are the Semantics of Javascripts If Statement

I always thought that an if statement essentially compared it's argument similar to == true. However the following experiment in Firebug confirmed my worst fears—after writing Javascript for 15 years I still have no clue WTF is going on:
>>> " " == true
false
>>> if(" ") console.log("wtf")
wtf
My worldview is in shambles here. I could run some experiments to learn more, but even then I would be losing sleep for fear of browser quirks. Is this in a spec somewhere? Is it consistent cross-browser? Will I ever master javascript?
"If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers; if either operand is a string, the other one is converted to a string."
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators
So the first one does:
Number(" ")==Number(true)
While the second one is evaluated like this:
if(Boolean(" ")==true) console.log("wtf")
I am guessing that it is the first part that is a problem, not the second.
It probably does some weird casting (most likely, true is cast to a string instead of " " being cast to a boolean value.
What does FireBug return for Boolean(" ") ?
JavaScript can be quirky with things like this. Note that JavaScript has == but also ===. I would have thought that
" " == true
would be true, but
" " === true
would be false. The === operator doesn't do conversions; it checks if the value and the type on both sides of the operator are the same. The == does convert 'truthy' values to true and 'falsy' values to false.
This might be the answer - from JavaScript Comparison Operators (Mozilla documentation):
Equal (==)
If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers; if either operand is a string, the other one is converted to a string
Highly recommended: Douglas Crockford on JavaScript.
Answer: aTruthyValue and true are not the same.
The semantic of the if statement is easy:
if(aTruthyValue) {
doThis
} else {
doThat
}
Now it's just the definition of what a truthy value is. A truthy value is, unfortunately, not something that is simply "== true" or "=== true".
ECMA-262 1.5
Setion 9.2 explains what values are truthy and which are not.
I recommend using === whenever possible, if only to avoid having existential crises.

Categories