Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean conditions in console.log() statement

I've just noticed that you can put boolean expressions within console.log e.g. console.log("hey" || 2) where in this casehey would be printed out to the console window.

I'm not 100% sure exactly how console.log determines what to print when there is a condition within it.

like image 727
breaktop Avatar asked Oct 21 '15 07:10

breaktop


People also ask

What is the output of console log boolean?

log() prints first truthy expression in your statement. If you were to try console. log(null || 2) , then 2 would be printed out.

What does console log () do?

The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console.

How do you declare a boolean in react JS?

Use the String() function to render a boolean value in JSX in React, e.g. <h2>{String(bool1)}</h2> . By default boolean values don't render anything in React, so we have to convert the value to a string in order to render it. Copied!

What boolean operators can be used in JavaScript?

There are three operators: AND, OR and NOT.

Does “console log()” return a Boolean?

As i know that console.log can return a result of a boolean, so i try to use it as an condition in “if ( )”. { “thank you!” { “thank you!” ==> “thank you!”// here is the result. i just want to know whether “console.log ()” returns a boolean or not, if there is a “5>2” or other equation.

Does console log() always return undefined?

Who told you that? console.log () always returns undefined, no matter what expression you give it as an argument. var result = console.log ("hello"); console.log (result); // ==> undefined console.log (console.log (true)); // ==> undefined var x = 0; if (console.log (33)) { x = 99; } console.log (x); // ==> 0

How to get the value of console log in JS?

in JS everything is a function. If you use it as a method, you'll still get undefined. The console shows the return value of your input. console.log () doesn't return anything, so undefined. You could just type directly into the console to get the result. This is because console.log () does not return a value (i.e. returns undefined).

What is a Boolean expression?

A Boolean expression is a C# expression that returns a Boolean value: True or False. You can use a comparison operator, such as the greater than (>) operator to find out if an expression (or a variable) is true:


2 Answers

There is a concept of truthy and falsy values in JavaScript. Non empty string is considered truthy value, so "hey" evaluates to true and is printed, because the part after || is not evaluated in that case.

In general truthy are all values that are not false, 0, "", null, undefined, or NaN.

MDN defines evaluation of OR expressions as follows:

Logical OR (||) expr1 || expr2: 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.

So by this logic console.log() prints first truthy expression in your statement. If you were to try console.log(null || 2), then 2 would be printed out.

like image 166
Bohuslav Burghardt Avatar answered Sep 25 '22 23:09

Bohuslav Burghardt


if the a value is falsy(false, undefined, 0, NaN and "") then it will take the right side value.else it will get print the a itself.

var a = null;

console.log(a || 10); //10 will print

or

var a = 20;

console.log(a || 10);//20 will print
like image 20
Ramasamy Kanna Avatar answered Sep 22 '22 23:09

Ramasamy Kanna