skip to Main Content

enter image description hereTry this in chrome console:

var x =2;
console.log(x);

Why console.log() on chrome console display undefined and does not print out value? Is there anything wrong in my setting?

What to do if I want to list a value on the console?

P.S. add an image to show the output on console

2

Answers


  1. Chosen as BEST ANSWER

    I reset the chrome inspector using the suggested method below and it works fine now.

    How to reset chrome inspector?


  2. Your code should be giving the below output in console –

    2
    undefined
    

    Browser console also prints return value of the last statement method or function call. Return value of console.log() is undefined so it prints undefined also.
    Try these cases –

    Case 1:

    function log(y){
        console.log(y);
        return y + 1;
    };
    var x = 2;
    log(2);
    

    Output –

    2 - console.log(y)
    3 - return value of log(x)
    

    Case 2:

    function log(y){
        console.log(y);
        return y + 1;
    };
    var x = 2;
    log(x);
    console.log(x);
    

    Output –

    2 - console.log(y)
    2 - console.log(x)
    undefined - return value of console.log(x)
    

    Case 3:

    function log(y){
        console.log(y);
        return y + 1;
    };
    var x = 2;
    console.log(x);
    log(x);
    

    Output –

    2 - console.log(x)
    2 - console.log(y)
    3 - return value of log(x)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search