JavaScript Foundations Practice Exercises
Variables, conditions, and basic logic
Challenges
Name a Fixed Value
Name a Fixed Value
Use `const` when a value should keep the same meaning for the rest of the progra...
Track a Status
Use `let` when a variable needs to hold one value now and a different value late...
Empty on Purpose
`null` means a developer deliberately put "nothing" into a variable. `undefined`...
Label Value Types
`typeof` tells you the type of a value. Store each type label in its own variabl...
Choose the Keyword
A subscription dashboard has a plan name that should stay fixed and a seat count...
Keep the Old Status
Sometimes you need both the old value and the new value. Save the old value befo...
Predict Reassignment
Read this code without running it: Your task: Set the answer variables in the...
Fix the Counter
This retry counter is supposed to increase twice and log `2`, but the current de...
Swap Two Values
Two server variables need to trade values. This is a common reassignment pattern...
Track a Balance
Practice JavaScript variables and arithmetic by tracking a balance that changes ...
Trace Empty Values
Practice JavaScript variables by tracing undefined, null, typeof, and reassignme...
Checkout Summary
Build a checkout summary with JavaScript variables, arithmetic, null state, and ...
Arithmetic Operators
Arithmetic operators create a new number from existing values. They do not chang...
Group the Math
JavaScript follows operator precedence. Multiplication runs before addition and ...
Remainder Operator
The remainder operator, `%`, gives back what is left after division. A number di...
Assignment Operators
Assignment operators update an existing variable from its current value. They ar...
Predict Increment
Increment operators update a number by one. The prefix form updates before the v...
Fix Plus Behavior
The `+` operator adds numbers, but it joins text when either side is a string. N...
Comparison and Logic
Practice JavaScript comparison and logical operators by modeling a workshop acce...
Fix the Average
Debug a JavaScript operator precedence bug in an average calculation. This mediu...
String Quotes
A string is text wrapped in quotes. If the text itself contains an apostrophe, c...
String Concatenation
The `+` operator joins strings. When you build readable text, remember that spac...
Template Literals
Template literals use backticks. They let you place values inside a string with ...
Length and Indexing
String indexes start at `0`, so the last character is one less than the string l...
Clean Input
User input often arrives with extra spaces or inconsistent casing. Cleaning the ...
Slice a String
`slice` returns part of a string without changing the original. The start index ...
Search Cleaned Text
Normalize an email with JavaScript string methods before checking whether it mat...
Mask a Preview
Use JavaScript string length, repeat, and slice to mask an access code while kee...
If Statements
An `if` statement runs a block only when its condition is true. That makes it us...
If and Else
`if` handles the true path. `else` handles everything that does not pass the con...
Else If Chains
An `else if` chain checks ordered conditions. Once one branch runs, the later br...
Logical Operators
Real conditions often combine multiple rules. `&&` requires both sides to be tru...
Truthy and Falsy
Conditionals do not only work with explicit booleans. Values like an empty strin...
Ternary Operator
A ternary expression chooses between two values. It is best for short decisions ...
Switch Statements
Practice JavaScript switch conditionals by mapping a user role to one dashboard ...
Boundary Conditions
Debug a JavaScript conditional boundary where a threshold score falls into the w...
Ordered Decisions
Practice JavaScript conditionals by classifying an order through ordered fulfill...
For Loops
A `for` loop is useful when you know how many times the loop should run. The loo...
Running Totals
A loop can update one variable over and over. This is how many totals, counters,...
While Loops
A `while` loop keeps running while its condition stays true. You must update som...
Continue
`continue` skips the rest of the current loop pass and moves to the next one. It...
Break
`break` exits a loop immediately when a search has found the value it needs, so ...
For...of
`for...of` loops over values directly, which makes it useful for reading each ch...
Nested Loops
Practice JavaScript nested loops by building ordered row-and-column grid labels,...
Off-by-One Bugs
Debug a JavaScript loop boundary that reads one step past the end of an array. T...
Declare and Call a Function
A function stores a block of code under a name. The code inside does not run unt...
Return Values
`return` sends a value back to the place where the function was called. Logging ...
Parameters
A parameter is a named input for a function. It lets one function work with diff...
Multiple Parameters
Functions can receive more than one input. The order of arguments matters becaus...
Default Parameters
A default parameter supplies a value when the caller leaves an argument out. Thi...
Arrow Functions
Arrow functions are function expressions with a shorter syntax. They are still v...
Return, Don't Log
Debug a JavaScript function that logs a calculated value but must return the num...
Rest Parameters
Practice JavaScript rest parameters by gathering flexible score arguments into a...
Functions as Values
Practice JavaScript functions as values by passing an operation into a reusable ...
Function Scope
Debug JavaScript function scope by returning a locally built summary instead of ...
Arrays
An array stores ordered values under one name. The first item is at index `0`, n...
Last Item by Length
The final index in an array is always one less than the array length. That patte...
Push Mutates
`push` adds an item to the end of an existing array. It changes that array inste...
Slice Copies
`slice` returns a new array containing part of the original. It does not remove ...
Searching Arrays
Some array searches answer "where is it?" while others answer "is it here?". Use...
Loop Through an Array
Arrays are often processed one item at a time. A loop can read each value and up...
Array Boundaries
Practice JavaScript array boundary reads by writing a reusable helper that deriv...
Copy Before Changing
Practice JavaScript array copying by adding an item to a returned list while kee...
Mutation Return Values
Debug a JavaScript array mutation mistake by separating the item removed by pop ...
Objects
An object stores related values as properties. Dot notation reads a property by ...
Bracket Notation
Bracket notation reads the property named by an expression. That means a variabl...
Updating Properties
A `const` object variable cannot be reassigned, but its properties can still be ...
Adding Properties
Objects can gain new properties after they are created. This is useful when data...
Nested Objects
Objects can contain other objects. Reading nested data means following the prope...
Object Methods
A method is a function stored on an object. When a method needs other properties...
Object Keys and Values
Practice JavaScript Object.keys and Object.values by inspecting preference data ...
Copy Before Updating
Practice JavaScript object copying by returning an updated user record without m...
Object References
Debug a JavaScript object reference bug by copying saved profile data before edi...