Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this communicating: my_var = my_var || 69 [duplicate]

Tags:

javascript

I saw this in a Javascript example

my_var = my_var || 69

I assume it means to check if my_var exists, if not set my_var to 69. Is this the case? Is there any documentation on this, it is very hard to represent as a google/SO search, could somebody point me in the direction of docs or duplicate QA?

(The example didn't use 69, that's just me being crass)

like image 453
Dom Vinyard Avatar asked May 10 '13 09:05

Dom Vinyard


1 Answers

Easy enough to try in the JS console.

var my_var
my_var = my_var || 69
//69

var my_var = 5
my_var = my_var || 69
//5

You are setting the variable only if it is currently carrying a falsy value.

Falsy values in JS are:

  1. false
  2. null
  3. undefined
  4. The empty string ''
  5. The number 0
  6. The number NaN
like image 160
Anirudh Ramanathan Avatar answered Nov 08 '22 04:11

Anirudh Ramanathan