Reading history

JavaScript Introduction: What It Is and Your First Program

4 min read 3 views

Start learning JavaScript: what it is, where it runs, console.log(), adding scripts to a web page with the script tag, and basic syntax rules.

JavaScript is the programming language of the web. HTML gives a page its structure and CSS gives it style; JavaScript makes it respond: it reacts to clicks, checks forms, updates content without reloading and talks to servers. It also runs outside the browser through Node.js, so one language can power both the front end and the back end of a website.

In this lesson you will learn

  • What JavaScript is and where it runs
  • How to write your first program with console.log()
  • How to add JavaScript to a web page with <script>
  • Comments, statements and semicolons
  • How to open the browser console

Your First JavaScript Program

// My first JavaScript program
console.log("Hello, JavaScript!");
console.log("2 + 3 =", 2 + 3);

const name = "Rahim";
console.log("Welcome, " + name + "!");

Output

Hello, JavaScript!
2 + 3 = 5
Welcome, Rahim!
  • console.log() prints values to the console. It is the tool you will use most for checking what your code is doing.
  • You can pass several values separated by commas, and they are printed with spaces between them.
  • const name = "Rahim"; stores a value in a variable. The + operator joins strings together.
  • Lines that start with // are comments. JavaScript ignores them; they are notes for people.

Where to Run JavaScript

PlaceHow
Browser consolePress F12 (or Ctrl+Shift+J in Chrome), open the Console tab and type code directly. Quickest way to experiment.
Web pagePut code inside a <script> tag or in a separate .js file.
Node.jsInstall Node.js, save code as app.js, then run node app.js in a terminal. The console examples on this site were run this way.

JavaScript in a Web Page

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>First script</title>
</head>
<body>
  <h3 id="msg">Click the button.</h3>
  <button id="btn">Say hello</button>
  <script>
    const msg = document.getElementById("msg");
    document.getElementById("btn").addEventListener("click", function () {
      msg.textContent = "Hello from JavaScript! The time is " + new Date().toLocaleTimeString();
    });
  </script>
</body>
</html>

Result

  • The <script> element holds the JavaScript. It is placed at the end of <body> so the heading and button already exist when the code runs.
  • document.getElementById("msg") finds an element on the page. You will learn this properly in the DOM lesson.
  • addEventListener("click", ...) runs a function each time the button is clicked. Try it in the preview.

For real projects, keep JavaScript in its own file and link it:

<script src="app.js" defer></script>

The defer attribute lets the script load in the background and run only after the HTML has been read, so the tag can safely go in the <head>.

Statements, Semicolons and Case

  • Each instruction is a statement. Ending statements with ; is optional in most cases, but using it consistently avoids rare surprises.
  • JavaScript is case-sensitive: name, Name and NAME are three different variables, and Console.log is an error.
  • Comments come in two forms: // one line and /* several lines */.

JavaScript Is Not Java

Despite the similar names, JavaScript and Java are different languages with different designs and uses. The name was a marketing decision in 1995. The official standard behind JavaScript is called ECMAScript, and versions such as ES6 (2015) added much of the modern syntax used in these lessons.

Common Mistakes

  1. Putting the script in the head without defer, so it runs before the elements it needs exist.
  2. Wrong capitalisation, such as console.Log or getElementByID.
  3. Mismatched quotes or brackets. Read the error message in the console; it gives the line number.
  4. Ignoring the console. When nothing happens, an error is almost always shown there.

Practice Exercises

Exercise 1. Print your name and your age on one line.

Show answer
console.log("Name:", "Sabbir", "Age:", 19);

Exercise 2. Print the number of minutes in a week, letting JavaScript do the multiplication.

Show answer
console.log(7 * 24 * 60); // 10080

Frequently Asked Questions

Do I need to install anything to learn JavaScript?

No. Every web browser already runs JavaScript, so you can start in the browser console. Install Node.js later if you want to run scripts from files.

Should I learn HTML and CSS first?

It helps for the browser parts, such as the DOM and events. The core language (variables, loops, functions) can be learned on its own. See the Web Development lessons for HTML and CSS.

Summary

  • JavaScript adds behaviour to web pages and also runs on servers through Node.js.
  • console.log() prints values; the browser console shows output and errors.
  • Add scripts with <script>, ideally from a separate file with defer.
  • JavaScript is case-sensitive and is not related to Java.

Next: JavaScript Variables and Data Types.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *