FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

25 snippets foundClear filters
JavaScriptGetting Started

Calculate a simple total

JavaScript: Multiply a unit price by a quantity, then add a delivery fee.

JavaScript✓ Tested
JavaScriptGetting Started

Call a library function

JavaScript: Use a standard absolute-value function with a small negative number.

JavaScript✓ Tested
JavaScriptGetting Started

Choose between two branches

JavaScript: Use if and else to handle an available or empty stock count.

JavaScript✓ Tested
JavaScriptGetting Started

Combine text with a name

JavaScript: Construct a greeting from a fixed prefix and a stored name.

JavaScript✓ Tested
JavaScriptGetting Started

Compare a value with a limit

JavaScript: Turn a comparison into a readable yes or no result.

JavaScript✓ Tested
JavaScriptGetting Started

Convert known numeric text to an integer

JavaScript: Convert a trusted literal to an integer before adding. Validate external input before converting it.

JavaScript✓ Tested
JavaScriptGetting Started

Count the items in a collection

JavaScript: Read the collection’s length instead of hard-coding the loop limit.

JavaScript✓ Tested
JavaScriptGetting Started

Divide using a fractional number

JavaScript: Use a floating-point operand to preserve the fractional part of a division.

JavaScript✓ Tested
JavaScriptGetting Started

Explain a calculation with a comment

JavaScript: Keep the code executable while documenting why a fixed input is used.

JavaScript✓ Tested
JavaScriptGetting Started

Find what remains after division

JavaScript: Use the remainder operator to count items left after forming complete groups.

JavaScript✓ Tested
JavaScriptGetting Started

Handle three possible outcomes

JavaScript: Check high, normal and low values with an ordered conditional chain.

JavaScript✓ Tested
JavaScriptGetting Started

Measure a short text value

JavaScript: Read the length of an ASCII string. Non-ASCII text may use multiple code units or bytes depending on the language.

JavaScript✓ Tested