Skip to content

JavaScript console log variable and string | Example code

  • by

The JavaScript console log function has many ways to print variables and strings. The console log can take multiple arguments.

Method 1:

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

Benefit: if the name is a JSON object, it will not be printed as "story" [object Object] "story"

Method 2:

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

Method 3: When using ES6 as mentioned above

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

Benefit: No need for extra, or +

Method 4: %

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

Advantage: the string becomes more readable.

JavaScript console log variable and string

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
    var name = "The Mountain"
    
    console.log("story", name, "story");

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

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

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

  </script>

</body>
</html>

Output:

JavaScript console log variable and string

console.log(message) usage is simple: the argument message is logged to the console.

console.log('My message');// logs "My message"

const myVar = 12;
console.log(myVar);// logs 12

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. Required fields are marked *