No history yet

Introduction to JavaScript

What is JavaScript?

If a website were a house, HTML would be the frame and walls, and CSS would be the paint and furniture. JavaScript is the electricity. It's the language that makes websites interactive—powering everything from clickable buttons and animations to complex web applications.

Lesson image

JavaScript runs directly in your web browser, which means it can change what you see on a webpage without needing to reload it from a server. This ability to manipulate a webpage in real-time is what makes modern web experiences so dynamic and responsive.

A Quick History

In 1995, a programmer at Netscape named Brendan Eich created JavaScript in just 10 days. The goal was to create a simple scripting language for the Netscape Navigator browser to make web pages more lively. Originally named Mocha, then LiveScript, it was eventually renamed JavaScript as part of a marketing strategy to align with the popular Java language, even though the two are completely unrelated.

What started as a simple browser tool has grown into one of the most popular programming languages in the world. It’s no longer confined to the web browser. Technologies like Node.js allow developers to use JavaScript to build servers, command-line tools, and even desktop applications.

Core Building Blocks

Like any language, JavaScript has its own grammar and rules. Let's start with the most basic concept: variables. A variable is a named container for storing data.

variable

noun

A storage location, with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.

In modern JavaScript, we typically declare variables using the let and const keywords. Use let for values that might change, and const for values that should never change.

// Use 'let' for variables that might be reassigned.
let userScore = 100;
userScore = 125; // This works fine.

// Use 'const' for constants that won't be reassigned.
const birthYear = 1990;
// birthYear = 1991; // This would cause an error!

These variables can hold different types of data. Understanding these data types is key to working with JavaScript.

Data TypeDescriptionExample
StringA sequence of text"Hello, world!"
NumberAny number, including decimals42, 3.14159
BooleanA logical value of true or falsetrue
nullAn intentional absence of valuelet name = null;
undefinedA variable that has not been setlet age;

To work with these variables and their data, you'll use operators. These are symbols that perform specific operations, like arithmetic or comparisons.

// Arithmetic Operators
let totalCost = 50 + 10; // Addition -> 60
let itemsPerBox = 100 / 5; // Division -> 20

// Comparison Operators (result in a Boolean)
let canVote = userAge > 18; // Greater than -> true or false
let isSameUser = userId === "12345"; // Strict equality -> true or false

Controlling the Flow

Code doesn't always run straight from top to bottom. Often, you need it to make decisions or repeat an action. This is where control structures come in. The most common way to make a decision is with an if...else statement.

An if statement checks if a condition is true. If it is, a specific block of code runs. If not, the optional else block runs instead.

let temperature = 75;

if (temperature > 80) {
  console.log("It's hot outside!");
} else {
  console.log("It feels great today.");
}

When you need to repeat a task multiple times, you use a loop. The for loop is a common choice when you know how many times you want to repeat an action.

A for loop repeats a block of code a specific number of times. It initializes a counter, sets a condition to continue, and increments the counter after each pass.

// This loop will print numbers 1 through 5
for (let i = 1; i <= 5; i++) {
  console.log("Count is: " + i);
}

These fundamentals—variables, data types, operators, and control structures—are the foundation of all JavaScript programming. Mastering them is the first step toward building dynamic and interactive web experiences.

Quiz Questions 1/6

In the analogy of a website being a house, what does JavaScript represent?

Quiz Questions 2/6

Who created JavaScript and for which company?