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.
log() prints first truthy expression in your statement. If you were to try console. log(null || 2) , then 2 would be printed out.
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.
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!
There are three operators: AND, OR and NOT.
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.
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
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).
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:
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
: Returnsexpr1
if it can be converted to true; otherwise, returnsexpr2
. 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With