JS Coding Questions Logo
JS Coding Questions
#138💼 Interview💻 Code

How do you assign default values to variables

Advertisement

728x90

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

javascript
1var 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'.

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 54

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
138of476