First Steps in JS

JavaScript Logo

JavaScript Lessons

In this page we explain the basic ideas used in the examples.


Lesson 1: How to input data

We can read a number from the user using prompt and convert it to a number.

var num;

num = Number(prompt("Enter a number"));

document.write(num);

Lesson 2: Basic numeric operators

We can do a simple calculation on two numbers using the +,-,*,/,% operator.

var a = 4;
var b = 2;
var sum = a + b;

document.write("Sum = " + sum);

Lesson 3: Conditions

We can check if a grade is Pass or Fail using if and else.

var grade;

grade = Number(prompt("Enter your grade"));

if (grade >= 50) {
    document.write("Pass");
} else {
    document.write("Fail");
}

Lesson 4: Loops

A loop can repeat the same code many times. Here we display the numbers from 1 to 3.

var i;

for (i = 1; i <= 3; i++) {
    document.write(i + "<br>");
}

Lesson 5: How to display the output

We can display text on the web page using document.write.

document.write("Hello from JavaScript");