Use || operator notification

Javascript Code:

var a = (b) ? b : 40; 

It works, only NetBeans says: "Use the || operator (Column [where is there?])." I did not find an explanation.

What is it?

Thanks!

+6
source share
2 answers

If you are simply verifying the likelihood of b , you can do this:

 var a = b || 40; 

... which is shorter and (perhaps) more obvious. In JavaScript || is a short circuit operator. He returns the left side if it is true, otherwise it returns the right side. (i.e., it does not return a boolean if the input is not boolean).

If you want to determine if b really is, then you are better off:

 var a = (typeof b !== "undefined") ? b : 40; 
+6
source

Proceedings are the or operator. var a = b || 40 var a = b || 40 says that if b is not false, let a = b, otherwise 40.

+3
source

Source: https://habr.com/ru/post/916362/


All Articles