in Education by
This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0 inside of an if statement. Is there a performance benefit to replacing == with ===? Any performance improvement would be welcomed as many comparison operators exist. If no type conversion takes place, would there be a performance gain over ==? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The strict equality operator (===) behaves identically to the abstract equality operator (==) except no type conversion is done, and the types must be the same to be considered equal. Reference: Javascript Tutorial: Comparison Operators 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. To quote Douglas Crockford's excellent JavaScript: The Good Parts, JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases: '' == '0' // false 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true ' \t\r\n ' == 0 // true The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator. Update: A good point was brought up by @Casebash in the comments and in @Phillipe Laybaert's answer concerning objects. For objects, == and === act consistently with one another (except in a special case). var a = [1,2,3]; var b = [1,2,3]; var c = { x: 1, y: 2 }; var d = { x: 1, y: 2 }; var e = "text"; var f = "te" + "xt"; a == b // false a === b // false c == d // false c === d // false e == f // true e === f // true The special case is when you compare a primitive with an object that evaluates to the same primitive, due to its toString or valueOf method. For example, consider the comparison of a string primitive with a string object created using the String constructor. "abc" == new String("abc") // true "abc" === new String("abc") // false Here the == operator is checking the values of the two objects and returning true, but the === is seeing that they're not the same type and returning false. Which one is correct? That really depends on what you're trying to compare. My advice is to bypass the question entirely and just don't use the String constructor to create string objects from string literals. Reference http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

Related questions

0 votes
    Give one example of Not Equals assignment operator.plz tell fast and correct Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    What is the difference between equals() and method and == operator? Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    The JavaScript classes can be instantiated using _____ operator. (a) create (b) new (c) instantiate (d ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    I wrote javascript code to solve the following problem >>> Write a function exponentiate that accepts two ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    _____is the statement is used for making decisions based on comparisons? Select the correct answer from above options...
asked Dec 16, 2021 in Education by JackTerrance
0 votes
    8 is the statement is used for making decisions based on comparisons it’s true or false ? Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
0 votes
    You need to combine the first_name and last_name columns in the employees table and display the columns as a combined ... | and || Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    Which of these operator is used to generate an instance of an exception than can be thrown by using throw? ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    The Laplacian operator cannot be used in which one the following? (a) Two dimensional heat ... proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 11, 2021 in Education by JackTerrance
0 votes
    Which operator can be used to access union elements if union variable is a pointer variable?...
asked Jan 18, 2021 in Technology by JackTerrance
0 votes
    Which operator can be used to determine the size of a data type or variable?...
asked Jan 18, 2021 in Technology by JackTerrance
+1 vote
    Which operator can be used to access union elements if union variable is a pointer variable in C Programming ?...
asked Nov 9, 2020 in Technology by JackTerrance
+1 vote
    Which operator can be used to determine the size of a data type or variable in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
...