Comp-sci: fantastic JavaScript

Self-tutoring about programming languages: the tutor mentions a neat feature of JavaScript.

Scripting languages are great because they can do so much with so little. Therefore, Perl and Awk are a couple of favourites. What about JavaScript?

Let’s say , in JavaScript, var z=-3. Then (z? 10:20) will return 10. If z=0 or z is undefined, (z? 10:20) will return 20. ?: makes up the JavaScript conditional operator. The question mark evaluates if z is true (assigned nonzero value). If true, the number before the colon is returned, which is 10 in this case. However, if z is 0 or null, the number after the colon, 20, is returned. An error is returned is z was never defined.

Neat, eh?

Source:

developer.mozilla.org

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply