0

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

Please help 🙏

15th May 2021, 4:22 AM
Question Mark
Question Mark - avatar
27 ответов
+ 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)
15th May 2021, 4:46 AM
minirkk
minirkk - avatar
+ 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
17th May 2021, 12:04 AM
David Carroll
David Carroll - avatar
+ 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") };
15th May 2021, 7:07 AM
minirkk
minirkk - avatar
+ 2
rkk === doesnt compare between values and types, it compares between values and memory addresses: https://code.sololearn.com/c8KLIfkU64Wc/?ref=app
15th May 2021, 6:17 AM
Bot
Bot - avatar
+ 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! :)
17th May 2021, 8:04 AM
minirkk
minirkk - avatar
+ 1
var ask = prompt ("Class"); if (ask!==12){alert("Invalid");} else{alert("valid")}; It is not working
15th May 2021, 4:33 AM
Question Mark
Question Mark - avatar
+ 1
Question Mark rkk already solved your problem
15th May 2021, 6:31 AM
Bot
Bot - avatar
+ 1
you can use !== as different data types will have different addresses
15th May 2021, 6:41 AM
Bot
Bot - avatar
+ 1
Thanks
15th May 2021, 12:09 PM
Question Mark
Question Mark - avatar
+ 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.
17th May 2021, 6:49 AM
David Carroll
David Carroll - avatar
+ 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. 👌
17th May 2021, 7:53 AM
David Carroll
David Carroll - avatar
0
Thanks rkk
15th May 2021, 6:01 AM
Question Mark
Question Mark - avatar
0
But ask's value = the value we put in prompt
15th May 2021, 6:02 AM
Question Mark
Question Mark - avatar
0
So if we put 123 why it showing string.
15th May 2021, 6:11 AM
Question Mark
Question Mark - avatar
0
How this code can give appropriate answer .
15th May 2021, 6:12 AM
Question Mark
Question Mark - avatar
0
Can you help me solve my problem
15th May 2021, 6:18 AM
Question Mark
Question Mark - avatar
0
Compairing data type
15th May 2021, 6:33 AM
Question Mark
Question Mark - avatar
0
Thanks
16th May 2021, 10:02 PM
Asim Farheen ⭐⭐🤺👿👿
Asim  Farheen ⭐⭐🤺👿👿 - avatar
0
Why +" "+ operator use. Explain ?
17th May 2021, 4:10 AM
Pankaj Panchal
Pankaj Panchal - avatar
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.
17th May 2021, 7:00 AM
minirkk
minirkk - avatar