Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use JavaScript | | set default value

Shulou Source: shulou.com Published: 2022-06-01 04:21:47 09月18日 Update

Editor to share with you how to use JavaScript | | set the default value. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's take a look at it!

Use | | set the default value

There is a default parameter function in ES6. To simulate this feature in older browsers, you can use the | | (OR operator) and take the default value as its second argument. If the first parameter returns false, the second parameter will be returned as the default value. Take a look at this example:

Function User (name, age) {this.name = name | | "Oliver Queen"; this.age = age | | 27;} var user1 = new User (); console.log (user1.name); / / Oliver Queen console.log (user1.age); / / 27 var user2 = new User ("Barry Allen", 25); console.log (user2.name); / / Barry Allen console.log (user2.age) / / 25 above are all the contents of the article "how to use JavaScript | | set default values". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Parameters articles content functions not much examples most more browsers knowledge industry information information channels operators channels references learning help browsing computing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno NVidia Apple MariaDB