0 day streak
Code library

Code worth reading.

Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.

3495 snippets · 10 languages
▶ Start learning
JavaScript · BeginnerSnippet

Simple If-Else Control Flow

Control flow allows your program to make decisions. The 'if' block runs only if the condition in parentheses is true; otherwise, the 'else' block runs.

snippet.js
javascript
1
2
3
4
5
6
const score = 85;
if (score > 50) {
console.log("Passed!");
} else {
console.log("Try again.");
}
Control FlowSyntax
JavaScript · BeginnerSnippet

Working with Template Literals

Template literals (using backticks ``) allow you to easily embed variables inside strings using the ${variable} syntax, making code much cleaner than using the + operator.

snippet.js
javascript
1
2
3
const user = "Alice";
const greeting = `Hello, ${user}!`;
console.log(greeting);
SyntaxData Types
JavaScript · BeginnerSnippet

Basic Arrow Functions

Arrow functions provide a concise way to write functions. If the function only has one expression, you can even omit the 'return' keyword and curly braces.

snippet.js
javascript
1
2
const double = (number) => number * 2;
console.log(double(5));
FunctionsSyntax
JavaScript · BeginnerSnippet

Declaring Variables with let and const

In modern JavaScript, 'const' is used for values that stay the same, while 'let' is used for variables that will change later. Using 'const' by default is a best practice to prevent accidental reassignments.

snippet.js
javascript
1
2
3
const birthYear = 1990;
let currentAge = 34;
currentAge = 35;
SyntaxBest Practices
JavaScript · BeginnerSnippet

Adding Items to an Array

Arrays are used to store lists of data. The '.push()' method adds a new element to the end of the array, and '.length' tells you how many items are inside.

snippet.js
javascript
1
2
3
const colors = ["red", "green"];
colors.push("blue");
console.log(colors.length);
Arrays & ListsData Types
JavaScript · BeginnerSnippet

Iterating with for...of

The 'for...of' loop provides a clean and easy way to iterate over elements in an array or other iterable objects.

snippet.js
javascript
1
2
3
4
5
const fruits = ['Apple', 'Banana', 'Cherry'];
for (const fruit of fruits) {
console.log('I like ' + fruit);
}
Control FlowArrays & Lists
JavaScript · BeginnerSnippet

Named Function Declarations

A function declaration defines a reusable block of code with a name. Unlike arrow functions, these are hoisted, meaning they can be called before they are defined in the code.

snippet.js
javascript
1
2
3
4
5
function calculateTotal(price, tax) {
return price + tax;
}
const total = calculateTotal(100, 19);
FunctionsSyntax
JavaScript · BeginnerSnippet

String Length Property

Every string in JavaScript has a 'length' property that automatically tells you how many characters (including spaces) are in that string.

snippet.js
javascript
1
2
3
4
const username = 'Coder123';
const nameLength = username.length;
console.log(nameLength); // 8
Data TypesSyntax