Introduction

JavaScript runs in browsers and servers, allowing developers to create dynamic experiences.

It provides objects, functions, and statements that form the basis of modern web applications.

What you should already know

Before learning JavaScript, you should have a basic understanding of HTML and the web.

  • Basic knowledge of websites
  • Familiarity with HTML markup
  • Some programming experience helps

JavaScript and Java

JavaScript and Java share some syntax, but they are different languages.

JavaScript is dynamically typed, while Java is statically typed and uses classes more explicitly.

Hello world

Use the browser console or an editor to run your first script.

function greetMe(name) {
  alert("Hello " + name);
}

greetMe("World");

Variables

Variables store values that may change during program execution.

let count = 0;
const pi = 3.14;

Declaring variables

You can declare variables with var, let, or const.

var x = 42;
let y = 13;
const z = 7;

Variable scope

Variables declared inside a function are local to that function.

function test() { let localValue = 1; return localValue; }

Global variables

Global variables are available throughout a script or window context.

window.userName = "Ada";

Constants

Constants cannot be reassigned after declaration.

const PI = 3.14;

Data types

JavaScript has primitive types like string, number, boolean, null, undefined, and symbol.

  • String
  • Number
  • Boolean
  • Null
  • Undefined

if...else statement

Use if/else to make decisions in your code.

if (age >= 18) { console.log("Adult"); } else {
  console.log("Minor"); }

while statement

A while loop repeats while a condition is true.

let n = 0; while (n < 3) { n++; }

Function declarations

Functions group reusable logic and make code easier to maintain.

function square(number) { return number * number; }

Reference

All the documentation on this page is adapted from MDN Web Docs.