Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript5 tutorials
  • Node.js Introduction2 tutorials
  • JavascriptJavascript · Beginner · Mid
  • JavascriptJavascript · Beginner · Short
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 2

Your First JavaScript: Running Code in the Browser

JavaScript is a programming language. A programming language is a set of instructions you write so a computer can do things. Browsers like Chrome, Firefox, and Safari all understand JavaScript. That makes it the main language of web pages.
Level: BeginnerLength: Mid2 min read

Read at another depth

Your First JavaScript: Running Code in the Browser

JavaScript is a programming language. A programming language is a set of instructions you write so a computer can do things. Browsers like Chrome, Firefox, and Safari all understand JavaScript. That makes it the main language of web pages.

In this chapter, you will run your very first JavaScript code. No setup needed — just a browser.

Where You Type JavaScript

Every modern browser has a developer console. The console is a place where you can type JavaScript and see the result immediately.

To open it:

  1. Right-click anywhere on a web page.
  2. Click Inspect.
  3. In the panel that opens, click the Console tab.

You can also press F12 on Windows, or Option + Command + J on a Mac.

Your First Line of Code

Click in the console, type this, and press Enter:

Code example in javascript
console.log("Hello, world!");

You should see Hello, world! printed back to you.

Here is what that line does:

  • console.log(...) is a function call. A function is a named piece of code that does a job. This one prints things to the console.
  • "Hello, world!" is a string. A string is text wrapped in quotes.

Try changing the text between the quotes and running it again. You can also do math:

Code example in javascript
console.log(2 + 3);
console.log("2 + 3");

The first prints 5. The second prints the literal text 2 + 3, because quotes mean "this is text, do not calculate it."

Three Ways JavaScript Runs

JavaScript code can live in a few places. It is worth knowing all three.

  1. The console — great for quick tests, like this chapter.
  2. Inline in HTML — code written inside a <script> tag in an HTML file.
  3. A separate file — a file like script.js linked from your HTML with <script src="script.js"></script>. This is how real websites do it.
Code example in html
<!DOCTYPE html>
<html>
  <body>
    <h1>My page</h1>
    <script src="script.js"></script>
  </body>
</html>

The browser loads the page, finds the script, and runs it from top to bottom, one line at a time. This matters: if line 1 has an error, the browser stops there and prints an error to the console.

A flow diagram. HTML loads, then the browser finds the script tag, then reads the JavaScript line by line, then outputs results or errors to the console.
100%
How a browser runs your script
Show diagram source
graph LR
  A[Browser loads HTML] --> B[Finds script tag]
  B --> C[Reads JS line by line]
  C --> D{Works?}
  D -- yes --> E[Output appears]
  D -- no --> F[Error in console]

Experiment Freely

The console is safe. You cannot break your computer or the website you are on — refresh the page and everything resets. Try arithmetic, strings, and console.log with different things. Mistyping is how you learn.

Exercise

You run console.log("5 * 5"). What is printed, and why?

Show answer

The text 5 * 5. Because it is inside quotes, it is a string (text), not a calculation. Removing the quotes would print 25.

Exercise

Where do you look in the browser to see JavaScript error messages?

Show answer

The developer console (F12, or Inspect > Console). Errors appear there in red.

Spotted a mistake, or code that does not work?

Report an issue

Chapter: Your First JavaScript: Running Code in the Browser

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that