How to use JavaScript | | set default value
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!