最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Boolean conditions in console.log() statement - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Oct 21, 2015 at 7:23 breaktopbreaktop 2,0296 gold badges42 silver badges62 bronze badges 1
  • If the expression at the left side of || resolves to something truthy, it will get printed, if not the expression on the right side gets printed. – Lars Juel Jensen Commented Oct 21, 2015 at 7:28
Add a comment  | 

4 Answers 4

Reset to default 15

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.

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

Most simply put, when a logical operator exists in the expression, it will try to evaluate the whole expression - and since "||" means "or", when "hey" is evaluated, it returns true, hence there is no need to evaluate the expression further.

Well, let me try to add my two cents to Bohuslav's great explanation... If you want to see a good practical example, check out this exercise from the book Eloquent Javascript:

"FizzBuzz

Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.

When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those)."

And the solution:

for (var n = 1; n <= 100; n++) {
    var output = "";
    if (n % 3 == 0)
        output += "Fizz";
    if (n % 5 == 0)
        output += "Buzz";
    console.log(output || n);
}

Here the variable output is an empty string, so the console log won't print it, as empty strings are falsy; but as soon as "Fizz" or "Buzz" is added to its value, it becomes truthy and gets printed out instead of n.

Note that it wouldn't work the other way around with: console.log(n || output). As n is the first expression and always true, it will be printed every time and output will be ignored.

发布评论

评论列表(0)

  1. 暂无评论