what is the difference between != and !== in Javascript? [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Are != and !== are respectively the same as == and ===?

!== and === are strict comparison, and == / != are loose comparison. It's best to use strict comparison.

true == 1 gives you true
true === 1 gives you false
Reason is that == compares only the value (so that 1, '1' is considered as true)
=== compares the value and the type.
Same thing in PHP.

== compares the value of object while === compares the object value and type.

yes it is.
<script>
var str = '1234';
var int = parseInt('1234');
if (int !== str)
{
alert('returns true and alerts');
}
if (int === str)
{
alert('returns false');
}
</script>
http://sandbox.phpcode.eu/g/c801e.php

Related

what is the difference between (!a==b) and (a != b) in JS? [duplicate]

This question already has answers here:
The negation operator in JS
(1 answer)
Should I use !== or ==! when checking for not equal?
(2 answers)
What is an exclamation point in JavaScript?
(2 answers)
What is the difference between != and !== operators in JavaScript?
(3 answers)
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 11 months ago.
if(openmenu != para){
addClass(openmenu , 'collapsed');
}
I am a beginner of JS, I want to add a classname to a element after a if statement. However, if(openmenu != para) and if(!openmenu == para) showed completely different result. Does anyone know why?
!openmenu == para is parsed as (!openmenu) == para, so it first negates openmenu and then checks whether it is equal to para, which is most likely not what you want to do. You could write !(openmenu == para) which would mean the same thing as openmenu != para.
!a == b inverts a's value. For example if it's true, using ! will invert it to false.
if(!true == false) equals to if(false == false) which results as true.
but if(a != b) compares values, lets say a is true and b is false, so if(true == false) => false values are not the same. :)
In this case, if a==null and b==false
so !a = true.
Then !a == b is false but a!=b is true.
I hope that answer is helpful to you.
You are comparing different values. When you add ! in front it toggles the value between true and false
const openmenu = 1;
const para = 2;
console.log(!openmenu) // toggles to false
console.log(!para) // toggles to false
console.log(!openmenu == para) // false == 2
console.log(openmenu != para) // 1 != 2
console.log(!"Turtle") // converts to false

Advantages of "string" === "string" with typeof [duplicate]

This question already has answers here:
What's the reason to use === instead of == with typeof in Javascript?
(5 answers)
Closed 6 years ago.
In Javascript === compares type as well as value, where as == just compares value. In the following example, is there any advantage of using === over ==?
function roll( sides ){
if ( typeof sides === "undefined" ) {
sides = 6;
}
var result = Math.random() ;
result = result * sides ;
result = Math.floor(result) ;
return result ;
};
According to the typeof documentation it will only ever return a string. This leads me to believe utilizing === doesn't offer any advantages in this situation.
I imagine whoever wrote the tutorial I am following used === out of habit?
In some cases, === might give you a tiny (usually negligible) performance benefit over ==.
It's also safer to use ===, precisely because it also tests if type is identical.
For example, consider this :
TRUE === 1 and FALSE === 0 both are FALSE
TRUE == 1 and FALSE == 0 both are TRUE.
In most cases, you want the former and not the latter behavior.

Why is this '=' syntax incorrect in this js snippet?

var hungry = true;
var foodHere = true;
var eat = function() {
if (hungry && foodHere === true) {
return(true);
} else {
return(false);
}`
};
The first line is the correct syntax. For a long time I was just saying hungry && foodHere = true... and I couldn't figure out (and still don't understand) why that is wrong. I understand the difference between = (assignment) and === (equal to). I assigned the variables to be true initially, so aren't I asking in the if statement if that's what they're set to? Why am I setting the variables = to in the var definition, but then when checking them I'm using the === value?
= is only used to assign variables. === or == are used to compare. For a good example, we must look into comparison operators.
Syntax
The syntax of comparison operators is fairly simple, use them to evaluate expressions. The comparasin operators are:
=== //strict equality
== //Parsed or partial equality
> //Greater Than
< //Less than
>= //Greater than or equal to
<= //Less than or equal to
To properly use these, you must know the proper syntax. For example, I can't do something like:
if(true == 1 === true) //do something
as that would invalidate the code, and slow it down by much using ==, which brings me to my next section.
Equality
The two equality operators in JavaScript are == and ===. They do two very different things.
===
The strict equality (===) tests whether two values are exactly equivalent, both in type and value.
==
The Parsed equality (==) tests whether two values are equalivent in value, but parses to try and connect different types.
Inequality
There are 2 main inequality value in JavaScript (!==) they are pretty self explainatory based on the equalities (===, and ==)
here's a chart explaining the three.
1 0 true false null undefined ""
1 === !== == !== !== !== !==
0 !== === !== == == == !==
true == !== === !== !== !== !==
false !== == !== === == == ==
null !== == !== == == == ==
undefined !== == !== == == === !==
"" !== == !== == == !== ===
Adding onto what #jcollum said, = defines a variable value, and if(something === true) simplifies into if(something). Similarly, if(something === false) simplifies into if(!something).
You also need to do comparisons separately. if(7 & 6 < 10) returns false because it is the simplified version of if(7 === true && 6 < 10).
It turns:
hungry && foodHere === true
into
hungry && true
or just
hungry
Using the assignment operator instead of the comparison operator is stopping your logic from working correctly.

What's the easiest way to check if a javascript variable is a number and greater than zero? [duplicate]

This question already has answers here:
Validate decimal numbers in JavaScript - IsNumeric()
(52 answers)
Closed 8 years ago.
I coded this:
isNumberNotZero: function (num) {
// Return false if num is null, an empty string or zero
if (num === null || typeof num === "undefined" || (typeof num === "string" && num.length === 0) || num == 0) {
return false;
}
return true;
},
But is there an easier way to do this. The code I have does not seem very clean.
You are doing a lot of checking for things that it is not, when you only care if it is a number greater than zero.
Just:
return typeof a === "number" && a > 0
This is from an answer to the famous Validate numbers in JavaScript - IsNumeric() question:
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
Then your test is as simple as combining that function with a > 0:
return isNumber(input) && input > 0;
You could use a unary plus operator to convert to an integer, if you want to shorten the code.
This isn't as readable, and only works for integers (e.g., if your number is 0.5, this will treat it as a zero), but it's fast and concise.
function isNumberNotZero(num) {
return +num !== 0;
}

what is the "===", and whats the big difference between it and the "==" [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
whats does the === mean when working with jquery/javascript? and whats the difference between === and ==?
like i got this code
if ($this.val() != '' || ignore_empty === true) {
var result = validateForm($this);
if (result != 'skip') {
if (validateForm($this)) {
$input_container.removeClass('error').addClass('success');
}
else {
$input_container.removeClass('success').addClass('error');
}
}
}
and there is the ===
i just want to understand what it does and whats the difference.
thanks
Both are equals operators. but === is type safe.
== This is the equal operator and returns a boolean true if both the
operands are equal.
=== This is the strict equal operator and only returns a Boolean true
if both the operands are equal and of the SAME TYPE.
for example:
3 == "3" (int == string) results in true
3 === "3" (int === string) results in false
hope this helps
== converts data to one type before comparing, === returns false if data of different types. === is preferrable.
Loosely speaking, it provides stricter comparison.
"0" == 0 // true
"0" === 0 // false
For example...
For further info, I recommend following the link Magnus provided.

Categories

Resources