...
Sign in

Writing and Running Your First JavaScript Program

Learn how to execute JavaScript code in your browser's Developer Tools Console and link scripts inside HTML files.

Share:

Writing and Running Your First JavaScript Program

You don't need to install complicated software or configure heavy servers to start programming in JavaScript.

Because you are using a web browser right now, you already have a full JavaScript development environment installed on your computer!

By the end of this lesson, you will know:

  • How to open and run code in your Browser Console
  • How to link JavaScript to an HTML page using the <script> tag
  • How JavaScript performs math and text operations
  • How to write single-line and multi-line comments

Method 1: The Browser Developer Tools Console 🛠️

Every major desktop web browser (Chrome, Edge, Firefox, Safari) includes built-in Developer Tools.

How to Open the Console:

  1. Press F12 (or Ctrl + Shift + I on Windows / Cmd + Option + I on Mac).
  2. Click on the Console tab at the top.
  3. You will see a blinking cursor next to a blue prompt (> ).
  4. Type:
    console.log("I am learning JavaScript!");
  5. Press Enter.

Instantly, the browser runs your code and prints:

I am learning JavaScript!

You can even use the Console as an instant calculator:

10 + 25 * 2;// Returns: 60

Method 2: Adding JavaScript to an HTML File 📄

In real-world web development, your JavaScript code lives inside an HTML document. You embed or link it using the <script> tag.

Inline Script Example:

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>My First JavaScript Page</title></head><body>  <h1>Welcome to ThinkWisely</h1>  <button onclick="sayHello()">Click Me!</button>  <script>    function sayHello() {      alert("Hello from JavaScript! 🎉");    }  </script></body></html>

When a user clicks the button, the browser triggers the sayHello() function, creating an interactive pop-up alert!

External Script Example (Best Practice):

As projects grow larger, it is best practice to keep HTML and JavaScript in separate files:

<!-- index.html --><script src="script.js"></script>
// script.jsconsole.log("External JavaScript script loaded successfully!");

Performing Basic Math in JavaScript 🔢

JavaScript includes standard arithmetic operators:

console.log(10 + 5);  // Addition: 15console.log(20 - 8);  // Subtraction: 12console.log(4 * 6);   // Multiplication: 24console.log(100 / 4); // Division: 25console.log(17 % 5);  // Modulo (Remainder): 2console.log(2 ** 3);  // Exponentiation (2 to the power of 3): 8

JavaScript strictly follows the standard mathematical order of operations (PEMDAS / BODMAS):

console.log(5 + 2 * 10);   // 25 (Multiplication happens first)console.log((5 + 2) * 10); // 70 (Parentheses override precedence)

Working with Text (Strings) 💬

Text in JavaScript is called a String. You can create strings using double quotes (""), single quotes (''), or backticks ( ):

console.log("Hello, World!");console.log('JavaScript is fun!');

String Concatenation:

You can join strings together using the + operator:

console.log("Welcome, " + "Aarav" + "!");// Output: Welcome, Aarav!

Code Comments: Leaving Notes for Humans 📝

Comments are lines of text that the JavaScript engine completely ignores. They exist solely to explain code to yourself and other developers:

Single-Line Comment:

Starts with two forward slashes //:

// This calculates the total price with standard 18% GSTlet total = 500 * 1.18;

Multi-Line Comment:

Enclosed between /* and */:

/*  This module processes user authentication.  Author: ThinkWisely Curriculum*/

[!TIP] Key Takeaways

  • You can test JavaScript instantly in any browser by pressing F12 and opening the Console.
  • Use the <script> tag to link JavaScript files to your HTML web pages.
  • JavaScript supports standard math operations (+, -, *, /, %, **).
  • Use // for single-line comments and /* ... */ for multi-line documentation notes.

CheckpointCheckpoint
Question 1 of 3Score: 0/3

Which keyboard shortcut opens the Developer Tools Console in most web browsers on Windows?

Lesson Milestone

Finished Reading This Lesson?

Mark this lesson complete. A chapter counts as done once all its lessons are, and that adds to your course progress.

TH

Written by

ThinkWisely Team

Have a Question or Suggestion?

We'd love to hear from you. Drop your thoughts below to help us improve ThinkWisely!