![]() Another good follow-up question is what would be result of comparing null and undefined using = and = operator in JavaScript. Similarly two String are equal to each other if contents of each others are same e.g. In fact, you should always use "=" operator for comparing variables or just for any comparison.ģ) While comparing variable using strict equality operator in Java, two object are strictly equal to each other if both are of same type and they refer to same instance. It's much similar Java's equality operator (=), which gives compilation error if you compare two variables, whose types are not compatible to each other. On the other hand = is known as strictly equality operator. We will see couple of example of both operator in this article, to understand difference between them much better.ĭifference between = and = in JavaScriptĠ= false // true, because false is equivalent of 0 0= false // false, because both operands are of different type 2= "2" // true, auto type coercion, string converted into number 2= "2" // false, since both operands are not of same typeĢ) "=" operator is known as type coercion operator and anytime if both values are same and compared using =operator, type coercion happens. if you compare a number with a string with numeric literal, = allows that, but = doesn't allow that, because it not only checks the value but also type of two variable, if two variables are not of the same type "=" return false, while "=" return true. In one word, main difference between "=" and "=" operator is that formerly compares variable by making type correction e.g. ![]() He asked to me as well, and that's the reason of this post. Though It did not affect his interview performance much, he was keen to know about this as soon as he finished his interview. He did the right think, politely said that he is not aware of the difference between = and = operator. In one of the recent JavaScript interview for a Java web development position, one of my readers was asked this questions, What is the difference between comparing variables in JavaScript using "=" and "=" operator? My reader got shocked because he was from Java background and doesn't have great exposure to JavaScript, though he was pretty much familiar with some JavaScript function, event handling, and some jQuery tricks, he wasn't aware of subtle details of JavaScript.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |