What is the meaning of this condition in java script?
How does it work?
function displayunicode(e){
var unicode = e.keyCode ? e.keyCode : e.charCode
alert(unicode)
}
What is the meaning of this condition in java script?
How does it work?
function displayunicode(e){
var unicode = e.keyCode ? e.keyCode : e.charCode
alert(unicode)
}
Shorthand for:
var unicode;
if (e.keyCode)
unicode = e.keyCode
else
unicode = e.charCode
alert(unicode);
You can even write it as:
var unicode = e.keyCode || e.charCode;
in keypress event in javascript you have this stuff, in some browsers you just have the e.keyCode in your event object, which here is e, and in some other browsers you have e.charCode instead. both keyCode and charCode refers to the key which is pressed.
and as @SB. has pointed out:
e.keyCode ? e.keyCode : e.charCode
means exactly:
var unicode;
if (e.keyCode)
unicode = e.keyCode
else
unicode = e.charCode
and as I have said this piece of code wants to get the key which has been pressed in keypress event.
It is just a simple inline if, have a look at this to understand how it works: How to write an inline IF statement in JavaScript?