Back
Interactive Explainer

JavaScript basics

Variables, functions, and the DOM: adding behavior to the web.

JavaScript basics

Variables, functions, and the DOM: adding behavior to the web.

~2 min read
1 learner completed

Variables and types

JavaScript is dynamically typed. You declare variables with let (block-scoped), const (block-scoped, cannot reassign), or var (function-scoped, avoid in new code). Types include number, string, boolean, null, undefined, object, and symbol; typeof and strict equality (===) help you reason about values.

Arrays and objects are reference types. Use const for arrays/objects when you do not reassign the variable; you can still mutate the contents (push, property assignment).

Functions and scope

Functions are first-class: you can pass them as arguments, return them, and store them in variables. Function declarations are hoisted; arrow functions (() => {}) are not and inherit this from the enclosing scope. Use return to produce a value; undefined is returned if you do not.

Scope: global, function, and block (with let/const). Closures occur when a function keeps access to variables from its creation scope—essential for callbacks, event handlers, and encapsulation.

The DOM and events

The Document Object Model (DOM) is the tree of elements in the page. You select elements (querySelector, getElementById, querySelectorAll), read and change properties (textContent, innerHTML, classList, style), and add or remove nodes.

Events (click, submit, keydown, etc.) are handled with addEventListener. Event objects carry target, preventDefault(), and stopPropagation(). Use event delegation when you have many similar elements to avoid attaching a listener to each.

Related concepts

Explore topics that connect to this one.

Suggested next

Often learned after this topic.

JavaScript Events and Async

Ready to see how this works in the cloud?

Switch to Career Paths for structured paths (e.g. Developer, DevOps) and provider-specific lessons.

View role-based paths

Sign in to track your progress and mark lessons complete.

Discussion

Questions? Discuss in the community or start a thread below.

Join Discord

In-app Q&A

Sign in to start or join a thread.