No history yet

Introduction to JavaScript

What Is JavaScript?

JavaScript is the programming language that brings websites to life. While HTML structures the content (like the skeleton) and CSS styles it (like the clothes), JavaScript adds interactivity (like the nervous system). It handles everything from simple pop-up alerts to complex features like interactive maps and animated graphics.

Think of it this way: if a webpage were a car, HTML would be the frame, CSS would be the paint job, and JavaScript would be the engine that makes it go.

Lesson image

Learning JavaScript means learning how to give instructions to a web browser. These instructions follow a specific set of rules, known as syntax.

Syntax and Data

At its core, programming is about storing and manipulating information. In JavaScript, we store information in variables. Think of a variable as a labeled box where you can put something. You declare a variable using the keywords let or const.

// Use 'let' for variables that might change.
let userAge = 30;

// Use 'const' for variables that won't change.
const birthday = "January 1st";

The information you store has a type. JavaScript has several fundamental data types to work with.

Data TypeDescriptionExample
StringTextual data, wrapped in quotes."Hello, world!"
NumberNumeric data, including integers and decimals.42, 3.14
BooleanA logical value of either true or false.true
ObjectA collection of key-value pairs.{ name: "Alex", age: 25 }
ArrayAn ordered list of values.[ "apple", "banana", "cherry" ]
UndefinedA variable that has been declared but not assigned a value.let city;
NullAn intentional absence of any value.let score = null;

Controlling the Flow

Control structures allow you to make decisions and repeat actions in your code. They direct the "flow" of your program.

Conditional statements run code only if a certain condition is true. The most common is the if...else statement. It's like telling the computer: "If this is true, do this. Otherwise, do that."

let temperature = 25; // in Celsius

if (temperature > 30) {
  console.log("It's a hot day!");
} else if (temperature < 10) {
  console.log("It's cold, wear a jacket.");
} else {
  console.log("The weather is pleasant.");
}

Loops are used to execute a block of code repeatedly. A for loop is great when you know how many times you want to repeat an action.

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

This tells the program to start a counter i at 1, run the code as long as i is less than or equal to 5, and add 1 to i after each cycle.

Packaging Code with Functions

As your programs grow, you'll find yourself writing the same code over and over. Functions solve this by bundling a set of instructions into a reusable package. You can then "call" the function whenever you need to perform that specific task.

Lesson image

A function can take inputs, called parameters, to customize its behavior. When you call the function, the values you provide for those parameters are called arguments.

// This function calculates the area of a rectangle
function calculateArea(width, height) {
  let area = width * height;
  return area; // Sends the result back
}

// Call the function with arguments to get the result
let myArea = calculateArea(10, 5); // myArea is now 50

Here, width and height are the parameters. When we call calculateArea(10, 5), the numbers 10 and 5 are the arguments. The return keyword sends the calculated value back out of the function, so we can store it in the myArea variable.

Ready to check your understanding of these core concepts?

Quiz Questions 1/6

In the analogy of building a website, if HTML is the skeleton and CSS is the clothing, what is JavaScript?

Quiz Questions 2/6

Which keyword is used to declare a variable in JavaScript whose value is not expected to change?

Variables, data types, control structures, and functions are the essential pillars of JavaScript. Mastering them is the first big step toward building dynamic and interactive web experiences.