Ohad
Ohad

Reputation: 1651

what is the meaning of this condition in java script?

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)
}

Upvotes: 0

Views: 200

Answers (3)

Mehran Hatami
Mehran Hatami

Reputation: 12961

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.

Upvotes: 2

Christian Giupponi
Christian Giupponi

Reputation: 7618

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?

Upvotes: 0

NG.
NG.

Reputation: 22914

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;

Upvotes: 4

Related Questions