Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Shorthand - What Does the '||' Operator Mean When Used in an Assignment? [duplicate]

Tags:

javascript

I just took a look at this answer, and I noticed the following line of javascript code:

hrs = (hrs - 12) || 12;

My Question:

What does the '||' operator mean when used in an assignment?

like image 830
Jim G. Avatar asked Dec 22 '10 16:12

Jim G.


Video Answer


1 Answers

In this case, the code assigns 12 to hrs if hrs-12 = 0 (as JavaScript sees it, 0 = false).

More generally, it assigns the latter value to the variable if the former value evaluates to 0, the empty string, null, undefined, etc.

like image 106
nsdel Avatar answered Sep 18 '22 08:09

nsdel