InterviewHubPRO
Section Overview
Q142

Question 142 of 521

Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

27%
JavaScript Internals (521 Qs)•Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

How do you assign default values to variables

You can use the logical or operator || in an assignment expression to provide a default value. The syntax looks like as below,

javascript
  var a = b || c;

As per the above expression, variable 'a 'will get the value of 'c' only if 'b' is falsy (if is null, false, undefined, 0, empty string, or NaN), otherwise 'a' will get the value of 'b'.