Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical operator || in javascript, 0 stands for Boolean false?

I happened to know the following code

Here is the code, and very simple:

var test = 0 || -1 ;
console.log(test);

then the output in the console is -1

and somehow i am really new into the javascript,

all i think of is that the 0 stands for Boolean False in JS ,and so || operator seems to ignore the 0 and assign the value -1 to the variable

so am i right ? i just want a confirm

like image 541
Lien Avatar asked Mar 06 '12 06:03

Lien


People also ask

Is || A Boolean operator?

Even though the || operator can be used with operands that are not Boolean values, it can still be considered a boolean operator since its return value can always be converted to a boolean primitive.

What does || mean in JavaScript?

The Logical OR operator ( || ) is an operator that returns its first or second operand depending on whether the first is truthy. A "truthy" value means anything other than 0 , undefined , null , "" , or false .

What does the || operator do?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What is boolean logic in JavaScript?

Boolean is a datatype that returns either of two values i.e. true or false. In JavaScript, Boolean is used as a function to get the value of a variable, object, conditions, expressions, etc. in terms of true or false.


2 Answers

  • ||expr1 || expr2 (Logical OR)

    Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false..

  • &&expr1 && expr2 (Logical AND)

    Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

All values in Javascript are either "truthy" or "falsy".
The following values are equivalent to false in conditional statements:

  • false
  • null
  • undefined
  • The empty string "" (\ '')
  • The number 0
  • The number NaN

All other values are equivalent to true.


So... var test = 0 || -1 ; returns -1.

If it was var test = 0 || false || undefined || "" || 2 || -1 it would return 2


Logical operator on MDN

like image 133
gdoron is supporting Monica Avatar answered Sep 22 '22 04:09

gdoron is supporting Monica


You can use Nullish coalescing operator (??)

The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

Only null and undefined will be falsey. 0 will be considered true. But take care: an empty string will be considered true too!

console.log('0 ?? 1  ->', 0 ?? 1) // expected output: 0
console.log('null ?? 1  -> ', null ?? 1) // expected output: 1
console.log('undefined ?? 1  ->', undefined ?? 1) // expected output: 1
console.log('"" ?? 1  ->', "" ?? 1) // expected output: ""
like image 36
Meffe Avatar answered Sep 21 '22 04:09

Meffe