I want to aak that how can we use( !==) Not identical comparision in if conditions..java script | SoloLearn: Learn to code for FREE!

0

I want to aak that how can we use( !==) Not identical comparision in if conditions..java script

Please help 🙏

5/15/2021 4:22:08 AM

Question Mark

27 Answers

New Answer

+5

Your code is correct. What is the difference b/w != and !==? 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. Now if you see the data type of var ask(String) and 12(int) is different in your code. And this is the reason why we have our output as Invalid because what this strict comparison is doing is that it's checking for the value and type as well and hence we have the condition ask!==12 to be true(because even if ask's value is 12, it type is not integer) [CODE] var ask = prompt("Class"); if (ask!==12){alert("Invalid");} else{alert("valid")}; console.log(typeof ask) console.log(typeof 12)

+5

rkk Expanding on your explanation: ---- "Reason is that == compares only the value (so that 1, '1' is considered as true)." ---- ... Javascript will first, attempt to apply type coercion on the values based on a set of equality comparison rules. For example, when one value is a string and the other is a number, an attempt to convert the string to a number will occur and then compare both values as numbers. If the string fails to convert, it's automatically false. There are a set of rules for the various combinations of types that exist for this. The point is, type coercion will occur with loose equality (==) before the values are compared. Strict equality (===) does not use type coercion. Therefore, when both types are different, false is automatically returned. See link for more details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality

+3

Question Mark parseInt() is used for converting string to integer in JS check this JS code: var ask = parseInt(prompt("Class")); if (ask!==12){ alert("Invalid"); } else{ alert("valid") };

+2

rkk === doesnt compare between values and types, it compares between values and memory addresses: https://code.sololearn.com/c8KLIfkU64Wc/?ref=app

+2

David Carroll Sorry for I've missed your intentions there. I totally agree with you on this (didn't have this thought at first) "But... in doing so, it only enables this sort of behavior to be repeated while polluting the thread with off-topic chatter (like this response from me for example)." I assumed that Mr. Pankaj is little bit social anxious(just like I am) in posting his own question publicly and so I thought I should take the chance to answer him here itself if that helps him in clearing his doubt. I hope that @Question Mark won't mind that much. But I will try to follow your words and intentions from now on and will try my best to not answer offtopic just like I did here. So sorry once again! :)

+1

var ask = prompt ("Class"); if (ask!==12){alert("Invalid");} else{alert("valid")}; It is not working

+1

Question Mark rkk already solved your problem

+1

you can use !== as different data types will have different addresses

+1

+1

Pankaj Panchal You wrote: ---- Why +" "+ operator use. Explain? ---- Your question makes no sense and appears to be off topic for this Q&A post. Unless this is a relevant follow up to this thread, you should ask this in a new Q&A post. However, I recommend providing some context to help make it clearer what you're trying to ask.

+1

rkk I know you meant well by answering the off-topic question here. But... in doing so, it only enables this sort of behavior to be repeated while polluting the thread with off-topic chatter (like this response from me for example). 😉 The OP no longer has an incentive to comply because he got his answer. I also suspected he was asking about string concatenation. However, I wanted to avoid having him respond with some explanation in this thread with further off-topic discussions. Also, it would have been good practice for him to improve the quality of his question in his own post. I hope you can see how good intentions can have unexpected consequences. 👌

0

Thanks rkk

0

But ask's value = the value we put in prompt

0

So if we put 123 why it showing string.

0

How this code can give appropriate answer .

0

Can you help me solve my problem

0

Compairing data type

0

0

Why +" "+ operator use. Explain ?

0

Pankaj Panchal Why +" "+ operator use. Explain ? var firstName = "Pankaj"; var lastName = "Panchal"; // string concatenation var fullName = firstName + " " + lastName; console.log(fullName); Output:- Pankaj Panchal Here + " " + is use for separating the first and last name by a space. I hope this helps! I think David Carroll sir is right. It's inappropriate to post your doubts in someone's else post.