5 JavaScript Fundamentals

Build a foundation in JavaScript by connecting values and variables to functions, decisions, and loops.

JavaScript and program execution

JavaScript is a programming language browsers use to add behavior to web pages. A browser normally runs statements from top to bottom, but calls, conditions, and loops can change what runs and when. These fundamentals provide the building blocks for programs that calculate results and respond to page activity.

Values, types, and operators

A is data a program can use. JavaScript includes numbers, strings, and booleans, as well as null and undefined. Arrays and objects group values, and functions are callable values. JavaScript is dynamically typed, so a can hold values of different types at different times.

Operators combine or compare values. The + operator can add numbers or join strings. For comparisons, === checks equality without converting types first, making it easier to reason about the result than with ==.

Takeaway: Values carry the data a program works with, and operators let the program combine or compare that data.

Naming values with variables

A is a named binding that lets a program refer to a . Declare variables with const or let:

Use const when the binding will not be reassigned and let when it needs to change. Both are limited to the surrounding block, the code enclosed in braces. A const binding cannot be reassigned, although an object or array referred to by that binding can still have its contents changed. Older code may use var, which has broader scoping behavior.

Takeaway: Choose a declaration based on whether the binding itself needs reassignment; const does not make an object's contents immutable.

Reusable actions with functions

A groups instructions so they can be run by calling it. Functions can receive parameters and return a result. The is the 's named input; the argument is the supplied in the call.

Here, name is a and "Ari" is the argument. return sends a back and ends that call. A that does not return a produces undefined. Functions help divide an application into small, reusable actions.

Takeaway: A packages an action; parameters let it work with supplied inputs, and return provides its result.

Decisions and repetition

is the order in which instructions run. By default, statements run from top to bottom. Conditions choose between paths, while loops repeat work.

Use if and else to select a path based on a condition:

Other comparison operators include <, >, <=, and >=. Logical operators such as && (and), || (or), and ! (not) combine or reverse conditions.

A for loop is useful when repetitions are counted or their number is known. A while loop repeats as long as its condition remains true.

This loop initializes step, continues while its condition is true, and increases step after each iteration. A loop must be able to reach a point where its condition becomes false; otherwise, it may never end.

Takeaway: Conditions make choices, and loops repeat instructions; ensure loop conditions can eventually become false.

Putting the fundamentals together

The example combines an array, a , a , a loop, and a condition to count characters in a list of words:

The receives an array, visits each item, and adds the length of each non-empty string to total. It then returns the result, which the program displays in the browser console. This shows how named values, reusable instructions, decisions, and repetition work together.