In JavaScript what's the difference between Equal to ('==' & '===') [duplicate] - javascript

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 7 years ago.
I am trying to understand equality in JavaScript. Here is the code.
var x = prompt("What is 10 + 10");
if (x === 10) {
document.write("Correct")
}
else {
document.write("Incorrect")
}
Why wouldn't I make the equals sign like "===". So if "10" is equal("===") to "x"(user answer) then it should be correct right?
I searched on both Stack Overflow and W3Schools, but couldn't find what I was looking for. I guess I'm just nor getting this "true or false" thing. I mean this seems like a very simple equation. Help would be great thanks guys!

=== is strict type equality which compares by both value and type
== is non-strict type equality, which compares only by value.
In other words, == performs type conversion and then compares values for equality. Here are some examples
"3" == 3
=> true
Explanation: The string 3 is converted to the number 3, which is equal to 3.
"3" === 3
=> false
Explanation: The string is not converted to a number. Thus the string 3 does not equal the number 3.
In your example, incorrect would be written to the document. That is because the result of prompt returns a string, and you are performing strict equality with a number.
In your case, the interpreter sees it like this
if ("10" === 10) {
// does the string "10" equal the number 10? If so
document.write("Correct")
}
else {
// Hey, wait a minute. It doesn't equal the number. I should write "Incorrect" instead.
document.write("Incorrect")
}

In Javascript,
== means: is equivalent to
=== means: is identical to
When the value of x is "10", x is equivalent to 10.
But it isn't identical to 10.

Related

What is the difference and what are the "&" and "&&" operators used for? [duplicate]

This question already has answers here:
What's the difference between & and && in JavaScript?
(4 answers)
Closed 3 years ago.
I have two conditions that validate if "a" equals zero and "b" less than 5, both behave in the same way, but with different operators, is there any real difference between them?
var a = 0;
var b = 3;
if (!a & (b < 5)) {
}
if (!a && b < 5) {
}
The key difference is that the & operator is a bitwise operator, while the && operator is a logical operator.
Bitwise operators work on bits and perform "bit by bit" operations, they are applied to the bits of one or two operands. The & represents a bitwise AND operation- where A and B represent two inputs; both inputs must be true in order for C to be true.
So for instance in the example you provided you have:
if(0 & 1){
}
The result of the bitwise AND on 0 and 1 as inputs is 0 (false) because both inputs must be true for the output to be true.
The && operator is a logical operator, which is used to make a decision based on multiple conditions. It can apply to one or two operands each of which may be true or false.
if (!a && b < 5) {
}
The above still evaluates to false because both conditions must be met for the code inside the if statement to be executed. In addition to this, using the && operator will (depending on the language) short circuit, which means that the second condition of the if will only be evaluated if the outcome is not determined by the first condition.
So this expression will fail as soon as a is found to be zero (false) because there is no point in evaluating the second expression as the first expression had to be true. This is another difference as the bitwise & operator does not short circuit.

How to make 0 not equal to empty string in JavaScript [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 5 years ago.
I know the rules that 0 is equal to empty string '' in javascript. So how to make if statement to differ this? I mean, I have field where I insert amount value. For empty string I need one message to show but for 0 value another. Currently, I always catch 0 == ''.
if (amount == '') {
$("#validation-msg3").addClass("opux-is-visible");
return false;
}
else if (amount < 5) {
$("#validation-msg1").addClass("opux-is-visible");
return false;
}
EDIT. when I use === I do not get condition equals true when field is empty. What does it mean? My amount value is different type than empty string? I parse it like this:
var amount = parseInput($("#amount").val());
Just use strict compare === that will check value AND type of operands.
Use three equation signs. It checks the type too and 0 will be different from ''.
if (amount === '') {
$("#validation-msg3").addClass("opux-is-visible");
return false;
}

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.

How "==" works in JavaScript? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 9 years ago.
I found == is little confusing for newbies, So I want someone to explain how it works.
For example -
new String("a") == "a" and "a" == new String("a") are both true.
new String("a") == new String("a") is false.
Why?
== is called comparison/equality operator, it compares 2 values, but not their data types so for example
1 == '1' will return true, for stricter comparison, use === which will compare the data types too so 1 === '1' will return false
== is a comparison operator that means "equal to" but does not take variable typing into account.
=== is a stricter comparison operator that means "equal to and same type".
So if you have a string called numberStr with a value of 2 and an integer called numberInt with a value of 2, they will evaluate as follows:
numberStr == numberInt // evaluates to true
numberStr === numberInt // evaluates to false because types are different

Values comparison [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 8 years ago.
[4] === 4 // is: false
[4] == 4 // is: true
'0' == 0 // is: true
'0' === 0 // is: false
Can anyone give the exact reason for this?Also what exactly does strict equality operator do or need for comparision?I learned that type and value should be same for strict(===) operator.Is this what strict equality operator checks .If yes,than how equl to operator works?
== Compare values
=== Compare values and type
For example
[4] //turns into "4" when comparing
"4" == 4 //They are the same
"4" === 4 //The values are the same, but not the type
Reference: http://es5.github.io/#x11.9.4
http://i.stack.imgur.com/q13LO.png
The === operator also compares the type of the object.
So, in [4] === 4
[4] is an array, but 4 is a number, so that evaluates to false.
And in '0' === 0
'0' is a string, but 0 is a number, so that evaluates to false.
The === operator compares both type and value, making it a stricter check.
The == operator performs a less strict value based checked. It will in some cases consider values of different types "equal" Such examples are 0 vs '' or 0 vs "0"
The == operator will see 0 and '' as equal whereas the === operator will not treat them as equal since they are of different types.

Categories