The snippet library.
Small, practical examples. Search by task or narrow things down below.
Name a value that stays fixed
JavaScript: Use a constant declaration or the language’s constant naming convention for a fixed limit.
JavaScript✓ Tested
Print a number with two decimal places
JavaScript: Format a small decimal value consistently for display.
JavaScript✓ Tested
Read one character from ASCII text
JavaScript: Use a valid zero-based index to read the first character.
JavaScript✓ Tested
Read the beginning of a text value
JavaScript: Extract or display the first three ASCII characters from a known nonempty string.
JavaScript✓ Tested
Read the first collection element
JavaScript: Access index zero only after establishing that the collection is nonempty.
JavaScript✓ Tested
Repeat a fixed number of times
JavaScript: Use a counted loop to print three numbered steps.
JavaScript✓ Tested
Repeat while a condition holds
JavaScript: Count down while a positive number remains, updating the loop variable on each iteration.
JavaScript✓ Tested
Replace one collection element
JavaScript: Update an existing element at a known valid index.
JavaScript✓ Tested
Require two conditions to be true
JavaScript: Combine a lower and an upper bound to check whether a value is in range.
JavaScript✓ Tested
Run your first program
JavaScript: Start with a complete program that prints one line. Follow the run instructions below.
JavaScript✓ Tested
Store a value in a named variable
JavaScript: Give a value a descriptive name and print it.
JavaScript✓ Tested
Update a running count
JavaScript: Change a variable after its initial value has been assigned.
JavaScript✓ Tested