100+ JavaScript Examples: Learn by Code

Bobby Jul 13, 2026

JavaScript, a high-level and interpreted programming language, is one of the core technologies of the World Wide Web. It is used to create interactive web pages, and is also employed in server-side network programming with Node.js, game development, and mobile apps. Here, we'll delve into JavaScript examples that highlight its versatility and power.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Before we dive into the examples, it's important to understand that JavaScript is mutative and鑺序,which means the order in which you code matters. Let's kick off with some basics.

JavaScript Arrow Functions: Write Shorter & Cleaner Code πŸš€
JavaScript Arrow Functions: Write Shorter & Cleaner Code πŸš€

JavaScript Data Types

JavaScript has several data types, each with their unique characteristics and uses.

JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘

Here's a simple example showcasing declaration and usage of different data types:

```javascript let name = 'John Doe'; // String let age = 25; // Number let isEmployee = true; // Boolean let cars = ['Toyota', 'Honda', 'Ford']; // Array let car = {type:"Ford", model:"Mustang", year:1964}; // Object ```

JavaScript Operators

Java script string method
Java script string method

Operators perform operations on variables and values. JavaScript has several types of operators, including arithmetic, assignment, comparison, logical, and more.

Here's an example using the '+' operator for addition and the '===' operator for equality check:

```javascript let x = 5; let y = 3; console.log(x + y); // Outputs: 8 console.log(x === y); // Outputs: false ```

JavaScript Control Flow

JavaScript this Keyword: Master Context Like a Pro πŸš€
JavaScript this Keyword: Master Context Like a Pro πŸš€

JavaScript's control flow structures, like conditionals (if, else if, else, switch) and loops (for, while), facilitate decision-making and repetition in your code.

Here's an example using if and for loops:

```javascript for(let i = 0; i < 5; i++) { if(i === 2) { console.log("Middle value: " + i); } } ```

JavaScript Functions

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had

Functions in JavaScript are reusable pieces of code that you can define once and use numerous times.

Here's a simple function example that performs a greeting:

JavaScript String Methods Cheat Sheet πŸ“˜βœ¨
JavaScript String Methods Cheat Sheet πŸ“˜βœ¨
Callback
Callback
an image of a computer screen with headphones on it and another screenshot of the web page
an image of a computer screen with headphones on it and another screenshot of the web page
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
a laptop computer sitting on top of a desk next to a pink background with the words 17
a laptop computer sitting on top of a desk next to a pink background with the words 17
JavaScript Cheat Sheet πŸš€ | Complete JS Reference for Beginners & Developers
JavaScript Cheat Sheet πŸš€ | Complete JS Reference for Beginners & Developers
JavaScript Basics | Chapter 1 πŸ“– | Part 1
JavaScript Basics | Chapter 1 πŸ“– | Part 1
JavaScript Object keys() Method
JavaScript Object keys() Method
HTML input types
HTML input types
Input Events in JavaScript
Input Events in JavaScript
8 Essential JavaScript Array Methods Visual Guide
8 Essential JavaScript Array Methods Visual Guide
Javascript Cheet cheet
Javascript Cheet cheet
How to Create a Function in JavaScript: Step-by-Step Guide πŸš€
How to Create a Function in JavaScript: Step-by-Step Guide πŸš€
πŸ“˜ JavaScript Basics| Linking JS File to HTML | Chapter 2 πŸ“– Part 2
πŸ“˜ JavaScript Basics| Linking JS File to HTML | Chapter 2 πŸ“– Part 2
Drag and Drop JavaScript with Source Code
Drag and Drop JavaScript with Source Code
an image of a computer screen with the text,'java script arrays '
an image of a computer screen with the text,'java script arrays '
Finding Values in JavaScript Arrays
Finding Values in JavaScript Arrays
Javascript Fundamental
Javascript Fundamental
javascript cheetsheet
javascript cheetsheet

```javascript function greet(name) { console.log('Hello, ' + name + '!'); } greet('World'); // Outputs: Hello, World! ```

Arrow Functions

Arrow functions provide a shorter syntax to define functions and Lexically bind 'this' keyword.

Here's an example of an arrow function with parameters:

```javascript const greet = (name) => { console.log('Hello, ' + name + '!'); } greet('World'); // Outputs: Hello, World! ``` JavaScript's richness and adaptability are evident through these examples.