JavaScript console log variable | Example code

  • by

You can simply pass variable names into the console log method to print the value of it in JavaScript. If you want a variable with a string in the console log Then use + to combine strings:

console.log("story " + name + " story");

console.log takes multiple arguments, so just use:

console.log("story", name, "story");

When using ES6 as mentioned above

console.log(`story ${name} story`);

the string becomes more readable.

console.log('story %s story',name);

JavaScript console log variable

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>

    var myVar = 100;

    console.log(myVar); 
    console.log("myVar = " + myVar);

  </script>

</body>
</html>

Output:

JavaScript console log variable

Function variable

declare the variables you want to access both inside your function and in other functions on the outer scope.

For example, let’s say your code is like this.

var valueOne;
function setValue(){
    valueOne = 3;
}
function readValue(){
    console.log(valueOne);
}
setValue();
readValue();

Output: 3

You can log them with a pair of braces around them ({}), which will create an object with the name of the variable as the key:

function someFunction() {};

const someOtherFunction = () => {};

const someValue = 9;

console.log({someFunction});
console.log({someOtherFunction});
console.log({someValue});

const renamed = someFunction;

console.log({renamed})

Do comment if you have any doubts or suggestions on this JS console topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.