JavaScript on the browser console

using the browser's console as a code playground
// updated 2025-05-02 07:10

All modern internet browsers should now include a console, which we can usually access by:

  • right-clicking on any webpage
  • clicking "Inspect" (or similar)
  • accessing the "Console" tab (or similar)

Console as a code playground

In the console, we can run lines of JavaScript code to get a feel for the language:

We can start with the command: console.clear(). This will visually clear the console, by removing any warnings and errors that the webpage might have generated. Then, let us try the following by typing each in its own line:

  • 2+2
    • the console will respond with 4
  • const x = 1
    • here, we have set a variable named x to a number, 1
    • the console will respond with undefined
      • this seems weird but we will move on
  • x
    • here, we call the variable x
    • the console will respond with 1 which equals the number that we set it in the previous command

This is JavaScript at its most basic!

Console as a code testing facility

So, in the console, we can test any "one-liner" of JavaScript code. We can also test snippets of multiple lines once we have a feel for how it works.

If we have any doubts about how JavaScript works, the console is a great place for testing out the language!

⬅️ older (in textbook-javascript)
📒 JavaScript comments and whitespace
newer (in textbook-javascript) ➡️
JavaScript data types 📒
⬅️ older (in code)
📒 JavaScript comments and whitespace
newer (in code) ➡️
JavaScript data types 📒
⬅️ older (posts)
📒 JavaScript comments and whitespace
newer (posts) ➡️
JavaScript data types 📒