JavaScript Treehouse Club - MASH MASH - JavaScript Stuff You Can Change

Paola Ledesma
Paola Ledesma
2,453 Points

Hi there! I was wondering if there is any reason why we need to add || 4 in the 3rd line of the .js doco. Thanks heaps!

var num = num || 4

2 Answers

simhub
simhub
26,541 Points

Hi Paola,

the || symbol means OR.

 var num = num || 4  // If no number passed in, default to 4

the "num" variable will first look for a "num" parameter to be set "function random_number(num){...}" if it isn't set then it will set the num variable "var num = ..." equal 4.

Because the computer will say - hey! you wrote "var num =..." that means you're declaring a variable name "num". Oh! and you want me too set it equal to "4" if there is no parameter "num" set in the "random_number(num)" function.

Paola Ledesma
Paola Ledesma
2,453 Points

simhub , thanks a lot! Does this mean that "4" is an arbitrary default value that could be changed?