What is !== in javascript? [duplicate] - javascript

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 9 years ago.
The code is used to verify an image or cell selection. My question is
What is !== used for in the following function:
function checkSelected() {
var cellList,
selectedCell,
imgList,
selectedImg,
i
cellList = document.getElementById("imgTable")
cellList = cellList.getElementsByTagName("td")
imgList = document.getElementById("b_list")
imgList = imgList.getElementsByTagName("img")
if (cellList[0].style.border.indexOf("7px outset") !== -1) { selectedCell = cellList[0] }
if (selectedCell === undefined || selectedImg === undefined) { return }
selectedCell.style.backgroundImage = "url(" + selectedImg.src + ")"
selectedCell.firstChild.style.visibility = "hidden"
selectedCell.style.border = "1px solid"
selectedImg.style.border = "1px solid"
}

!== is a stricter inequality that does not perform any type coercion on the operands, compared to !=, which does perform type coercion.
So !== will return true if the operands are not equal and/or not of the same type.
In contrast != returns true if the operands are equal. If the operands are not of the same type, JavaScript will try to convert both operands into a form suitable for comparison. If the operands are objects, then JavaScript will compare their references (memory addresses). This is best demonstrated as follows:
"3" != 3; // returns false because JavaScript will perform
// type coercion and compare the values
"3" !== 3; // returns true because the first operand is a
// string whereas the second is an integer. Since
// they are of different types, they are not equal.
For more information, take a look at Comparison Operators on MDN.

It means "not strictly equal to", as opposed to != which means "not equal to".
There are two ways to check for equality: == and ===, which are "equals" and "strictly equals" respectively. To see the exact difference, check out this table.
!= and !== are simply the corresponding negations of those operations. So for example a !== b is the same as !(a === b)

Related

how to make to array data types(object) as strictly equal (===) in JavaScript

In my application I have to make two array datatypes(one is any[] and other is number[]) as equal using strictly equal.
my code is:
.component.ts
if (categoryIds === PhysicalPackageConst.nrtPatchCategory){
this.materialTypes = PhysicalPackageConst.nrtPatchMaterialType;
categoryIds = [];
}
In the above if condition it is showing as false if I make it as ===(if I use == it is showing the data(true) but not for ===)
package.constant.ts
export const PhysicalPackageConst = {
nrtGumCategory : [29],
nrtPatchCategory : [30]
So I want to make it as true for the above condition in strictly condition
Can anyone help me on this
Strict Equality Comparison (===) ("strict equality", "identity", "triple equals") : Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal. If the values have the same type, are not numbers, and have the same value, they're considered equal. Otherwise to compare value.
var num = 0;
var str = '0';
console.log(num === str); // false
Abstract Equality Comparison (==) ("loose equality", "double equals") : The behavior for performing loose equality using == is as follows. Loose equality compares two values for equality after converting both values to a common type. After conversions (one or both sides may undergo conversions), the final equality comparison is performed exactly as === performs it.
var num = 0;
var str = '0';
console.log(num === str); // true
Equality comparisons and sameness
For your problem, it's logic to get those result, because you need to cast value of array :any[] to number and make strict compare.
let categoryIds: any[] = [];
let nrtPatchCategory: number = 30;
// browse categoryIds arrays (you can use any other method like for ...)
categoryIds.forEach(categoryId => {
if (Number(categoryId) === nrtPatchCategory) {
...
}
});
Note: For more detail of forEach() Array.prototype.forEach()
Exemple:
console.log(2 === Number('3')); // false
console.log(3 === Number('3')); // true

Difference between exclamation equals sign and exlamation 2x equals sign when checking with null

What is the difference between next if statements in javascript when checking with null?
var a = "";
if( a != null ) // one equality sign
....
if( a !== null ) // two equality sign
....
When comparing to null I can't find any difference.
According to http://www.w3schools.com/js/js_comparisons.asp
!= - not equal
!== - not equal value or not equal type
In JavaScript, null has type: object (try yourself executing the following sentence typeof null).
That is, !== will check that a is also object before checking if the reference equals.
Actually you know that === and !== are meant to check that both left and right side of the equality have the same type without implicit conversions involved. For example:
"0" == 0 // true
"0" === 0 // false
Same reasoning works on null checking.
!= checks
negative equality
while !== checks for
negative identity
For example,
var a = "";
a != false; //returns false since "" is equal false
a !== false; //returns true since "" is not same as false
but if you are comparing it with null, then value will be true in both ocassion since "" is neither equal nor identical to null
There is no difference between them when comparing to null.
When we use strict equality (!==) it is obvious because they have different types, but when we use loose equality (!=) it is an important thing to remember about JavaScript language design.
Because of language design there are also some common questions:
How do you check for an empty string in JavaScript?
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
var a = "";
(1) if( a != null ) // one equality sign
Above condition(1) checks value only and not data-type, this will return true.
....
(2) if( a !== null ) // two equality sign
This checks value and data-type both, this will true as well.
To understand it more precisely,
var a = "1";
if( a == 1) {
alert("works and doesn't check data type string");
}
if( a === 1) {
alert('doesn't works because "1" is string');
}
if( a === "1") {
alert('works because "1" is string');
}
There is a difference if variable has value undefined:
var a = undefined;
if( a != null ) // doesn't pass
console.log("ok");
if( a !== null ) // passes
console.log("ok");
Got idea from reading this great post Why ==null, Not ===null. Also != is faster.

While Loops Syntax Errors

The course is asking me to form a while loop and I keep getting errors or infinite loops. What am I doing wrong?
var understand = true;
while(understand= true){
console.log("I'm learning while loops!");
understand = false;
}
You are using an assignment operator (=) and not an equals test (==).
Use: while(understand == true)
Or simplified: while(understand)
Update from comments:
=== means the value and the data type must be equal while == will attempt to convert them to the same type before comparison.
For example:
"3" == 3 // True (implicitly)
"3" === 3 // False because a string is not a number.
= means assignment, while == is comparison. So:
while(understand == true)
Also note that while and other branch structures, take conditions. Since this is a Boolean you can just use itself:
while(understand)
Also a note of the difference between == and === (strict comparison). The comparison == will attempt convert the two sides to the same data type before it compares the values. While strict comparison === does not, making it faster in most cases. So for example:
1 == "1" // This is true
1 === "1" // This is false

Why is isNaN("1") false? [duplicate]

This question already has answers here:
Why does isNaN(" ") (string with spaces) equal false?
(25 answers)
Closed 8 years ago.
I'm trying to write a simple test for the input of a function to determine if all of the inputs are numbers or not.
function numbers(){
for (var i = 0; i < arguments.length; i++) {
if (isNaN(arguments[i])) return false;
}
return true;
}
However, when I pass in a list of numbers as characters (eg. numbers("1", "2")) I get true instead of the expected false.
isNaN implicitly coerces the argument to Number, and then checks whether this coerced value is NaN.
See http://es5.github.io/#x15.1.2.4
That is, isNaN(foo) is equivalent to isNaN(Number(foo))
Code fix:
if (typeof arguments[i] !== 'number' || isNaN(arguments[i])) return false;
The second part of the condition is because typeof NaN === 'number'.
Your function might be a bit more readable in functional style, using ES5's Array#every method:
//returns whether all arguments are of type Number and not NaN
function numbers() {
return [].every.call(arguments, function(arg) {
return typeof arg === 'number' && !isNaN(arg);
});
}
isNan() will attempt to cast it to a number, and then check. I would use this instead
if(!isNaN(parseFloat(arguments[i])) && isFinite(arguments[i])){
return false;
}
Alternatively, if you're using jQuery, you could use its built-in $.isNumeric() function

In an if statement in javascript what is the difference between == and ===? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
In some JavaScript if statements I've seen the use of === as opposed to the standard ==.
What is the difference between these? and should I be using either one of them over the other?
e.g.
if (variable == 'string') {
return;
}
compared to:
if (variable === 'string') {
return;
}
=== also checks to be equal by type
For instance 1=="1" is true but 1==="1" is false
The === is a strict comparison (also checks type), whereas the == does a more relaxed comparison.
For instance:
var a = 'test';
if (a == true) {
// this will be true
}
if ( a === true) {
// this will not be true
}
Another example:
var b = '0';
if ( b == 0){
// this will be true
}
if ( b === 0 ){
// this will not be true
}
In particular it is very important when comparing falsy values. In Javascript all the following will be treated as false with relaxed comparison:
* false
* null
* undefined
* empty string ''
* number 0
* NaN

Categories