javascriptvariableslet

JavaScript- let showing name instead of value


I am a beginner and Started learning JavaScript today. I tried from a tutorial:

let a = 1 console.log('a')

Now,when I run it, the answer should be the value which is 1, but it shows me the name instead which is a.

What I am doing wrong?? I am using VsCode and console from chrome to see the results..

I tried:

let a = 1 console.log('a')

Google console shows me a instead of 1.


Solution

  • When you do, let a = 1;

    It creates a variable (named a) that is holding the value 1.

    Now, in order to access the value stored in a, you must do:

    console.log(a)

    You are doing console.log('a')

    The above statement prints out the character 'a' onto the console. Because whatever inside the console.log() is a string, and strings are directly logged onto the console.

    You can fix this by writing:

    console.log(a); Its output will be 1, because you have stored 1 in a.