What Is JavaScript?

What Is JavaScript?

A school needs to calculate and display a student's result. JavaScript is a programming language that describes those instructions; a JavaScript engine runs them within an environment such as a browser or Node.js.

No previous programming knowledge is required. We will build small school examples before working with web pages.

What you’ll learn

  • Distinguish the JavaScript language from browser and Node facilities.
  • Explain how a stored school mark becomes a displayed result.
  • Change the mark and predict which branch runs.

Work through the school example

This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.

const marks = 72; stores 72 under the name marks. marks >= 50 asks whether that number is at least 50. The ? : expression chooses Pass when the comparison is true and Practise otherwise. console.log displays the selected string. We will study each of these building blocks in the following lessons.

JavaScript · school.js · complete example

const marks = 72;
console.log(marks >= 50 ? "Pass" : "Practise");

Expected output

Pass
Run in JavaScript compiler →

From source to a result

JavaScript · source for this model

const marks = 72;
console.log(marks >= 50 ? "Pass" : "Practise");

This diagram follows the supplied example. It is an explanatory model, not a JavaScript engine or a timing guarantee.

From source to a result

  1. Source code

    A developer writes the school rule.

    marks = 72
  2. Engine and host

    The engine evaluates the rule; the host provides console output.

    72 >= 50 is true
  3. Displayed result

    The selected label is sent to the console.

    Pass
Follow the relationships and values in the example.

Introduction

JavaScript is more than just a scripting language — it’s the heartbeat of dynamic, interactive web experiences. From validating forms to creating immersive games and single-page applications, JavaScript is the bridge between static content and real-time user interaction.

What is JavaScript?

JavaScript is a high-level, interpreted programming language that runs in the browser. It allows developers to add behavior to websites, making them interactive and engaging. Originally created by Netscape in 1995, JavaScript has since become a cornerstone of web development, alongside HTML and CSS.

Why Use JavaScript?

  • To manipulate HTML and CSS dynamically (DOM manipulation)
  • To handle user inputs and respond to events
  • To fetch data from servers without reloading pages (AJAX)
  • To validate form inputs before submitting to the server
  • To build full-fledged front-end applications using frameworks like React, Angular, or Vue

JavaScript in Action – A Simple Example

document.getElementById("demo").innerHTML = "Hello, JavaScript!";

HTML Setup:


<p id="demo"></p>
<script>
  document.getElementById("demo").innerHTML = "Hello, JavaScript!";
</script>
Hello, JavaScript!

How JavaScript Works

JavaScript is executed by the browser's JavaScript engine. It follows a single-threaded, event-driven model, meaning it can run asynchronously without blocking the main thread.

Variables and Data Types

Variables can be declared using let, const, or var. Data types include:

  • String – Text like "Hello"
  • Number – 42, 3.14
  • Boolean – true or false
  • Array – A list: [1, 2, 3]
  • Object – A key-value pair: { name: "John" }
let name = "Alice";
let age = 30;
let isStudent = false;
let scores = [90, 85, 88];
let user = { name: "Alice", age: 30 };

Functions

Functions are blocks of reusable code that perform specific tasks.

function greet(name) {
  return "Hello, " + name + "!";
}
console.log(greet("Sam"));
Hello, Sam!

Conditionals and Loops

Control flow helps JavaScript make decisions and repeat tasks.

// Conditional
let score = 80;
if (score >= 50) {
  console.log("Pass");
} else {
  console.log("Fail");
}

// Loop
for (let i = 0; i < 3; i++) {
  console.log("Iteration", i);
}
Pass
Iteration 0
Iteration 1
Iteration 2

Events in JavaScript

JavaScript responds to user actions using events.


<button onclick="alert('Button clicked!')">Click Me</button>

DOM Manipulation

JavaScript can access and modify the entire structure of a web page using the Document Object Model (DOM).

document.querySelector("h1").style.color = "blue";

Advanced Topics (Brief Overview)

1. ES6+ Features

  • let and const for better variable scoping
  • Arrow functions: const add = (a, b) => a + b;
  • Template literals: `Hello, ${name}`
  • Destructuring and spread/rest operators

2. Asynchronous JavaScript

Using callbacks, promises, and async/await to handle asynchronous tasks.

async function fetchData() {
  let response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
  let data = await response.json();
  console.log(data);
}
fetchData();
{ userId: 1, id: 1, title: "delectus aut autem", completed: false }

3. Object-Oriented Programming (OOP)

class Person {
  constructor(name) {
    this.name = name;
  }

  greet() {
    return "Hi, I'm " + this.name;
  }
}

let p = new Person("Leo");
console.log(p.greet());
Hi, I'm Leo

Conclusion

JavaScript is the soul of modern web development. Whether you're validating forms, creating animations, or building complex applications, JavaScript gives you the power to craft seamless user experiences. Start small, experiment often, and gradually dive deeper — because every master coder once typed console.log("Hello, world") for the first time.

Recognize the important boundary

JavaScript and Java are different languages. The language does not supply a web page or a database automatically; its host provides those facilities.

Try a small variation

JavaScript · original source for practice

const marks = 72;
console.log(marks >= 50 ? "Pass" : "Practise");

Your task: Replace 72 with 40. Predict the result before running the edited program.

The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.

Worked answer and reasoning

40 does not reach the pass threshold of 50, so the other label is selected.

JavaScript · complete worked variation

const marks = 40;
console.log(marks >= 50 ? "Pass" : "Practise");

Expected output

Practise
Run in JavaScript compiler →

Predict the original program

JavaScript · original program for this question

const marks = 72;
console.log(marks >= 50 ? "Pass" : "Practise");
What does the original JavaScript program display, in order?
Choose one answer. You can try again.

Answer and explanation

Pass

A school needs to calculate and display a student's result. JavaScript is a programming language that describes those instructions; a JavaScript engine runs them within an environment such as a browser or Node.js. JavaScript and Java are different languages. The language does not supply a web page or a database automatically; its host provides those facilities.

Keep the useful idea

A school needs to calculate and display a student's result. JavaScript is a programming language that describes those instructions; a JavaScript engine runs them within an environment such as a browser or Node.js.

Check your variation against this explanation: 40 does not reach the pass threshold of 50, so the other label is selected.

Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Set Up and Run JavaScript →