Featured Article

Simple JavaScript Examples: Learn with Ease

Adam Jul 13, 2026

Welcome to a comprehensive guide on simple JavaScript examples. If you're new to JavaScript or want to brush up on the basics, you're in the right place. We'll explore this dynamic programming language through straightforward examples, helping you understand its fundamentals. Let's dive in!

JavaScript
JavaScript

JavaScript is a popular, versatile language used primarily for web development. It's relatively easy to learn, especially if you're familiar with other programming languages. Even if you're new to coding, we'll keep examples simple and intuitive.

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

Getting Started with JavaScript

Before we start, ensure you have a text editor or an Integrated Development Environment (IDE) like Visual Studio Code. You can write JavaScript in browser consoles, but for larger scripts, an IDE is more convenient.

Java script string method
Java script string method

Let's start with a 'Hello, World!' example, the classic programming greeting.

Writing Your First JavaScript Code

HTML input types
HTML input types

Open your text editor or IDE and write the following:

console.log("Hello, World!");

This code uses the `console.log()` function to output "Hello, World!" in the browser's developer console. To see the output, open your browser, inspect the page, and navigate to the 'Console' tab. Paste your code there and press Enter.

Variables and Data Types

JavaScript Basics for Beginners | Learn JS Step by Step
JavaScript Basics for Beginners | Learn JS Step by Step

Variables in JavaScript are containers for storing data. They're declared using the `let` or `var` keywords. Data types determine the kind of data a variable can hold. Here are some basic data types:

  1. Numbers: let x = 10;
  2. Strings: let name = "John Doe";
  3. Booleans: let isStudent = true;

Try declaring variables of different data types in the console and observe their outputs.

Using switch Statements in JavaScript
Using switch Statements in JavaScript

Operators and Control Structures

Operators perform actions on variables and values. Control structures manage the flow of your code. Let's explore a few examples.

a calculator is shown with the numbers below it
a calculator is shown with the numbers below it
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
Essential JavaScript String Methods for Beginners
Essential JavaScript String Methods for Beginners
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
Read Files In Javascript
Read Files In Javascript
JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
Javascript Cheet cheet
Javascript Cheet cheet
Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
Very Simple Accordion In Pure Javascript
Very Simple Accordion In Pure Javascript
Click Events in JavaScript
Click Events in JavaScript
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
Update HTML in DOM: Change Text Live
Update HTML in DOM: Change Text Live
JavaScript Basics | Chapter 1 📖 | Part 2
JavaScript Basics | Chapter 1 📖 | Part 2
the instructions for how to use an object in visual language, including text and numbers
the instructions for how to use an object in visual language, including text and numbers
Jsonpath in Javascript Functionality And Use Cases
Jsonpath in Javascript Functionality And Use Cases
Javascript Flashcards Web App
Javascript Flashcards Web App
Toggle Fullscreen Mode in Javascript
Toggle Fullscreen Mode in Javascript
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
Javascript PHP JSON Example
Javascript PHP JSON Example

Arithmetic Operators

Arithmetic operators include +, -, *, /, and %. Try these examples in the console:

let a = 10;
let b = 3;
console.log(a + b); // addition
console.log(a - b); // subtraction
console.log(a * b); // multiplication
console.log(a / b); // division
console.log(a % b); // modulus (remainder)

Control Structures: if and for

JavaScript's `if` statement executes a block of code if a condition is true. The `for` loop repeats a block of code while a condition is true. Here's an example:

let i = 0;
for (; i < 5; i++) {
  if (i === 3) {
    console.log("The number is 3");
  } else {
    console.log("The number is not 3");
  }
}

This script will print "The number is not 3" four times and "The number is 3" once.

Functions

Functions in JavaScript are reusable pieces of code. They're declared using the `function` keyword. Here's a simple function that greets a provided name:

Defining and Calling Functions

Write this function in the console:

function greet(name) {
  console.log("Hello, " + name);
}

Now, call the function with your name as an argument: greet("Jane Doe");

Arrow Functions

Arrow functions provide a concise syntax for defining functions. Here's how you can rewrite the previous function as an arrow function:

let greet = (name) => console.log("Hello, " + name);

Try this example in the console and observe that it behaves the same as the previous function.

That's it for our simple JavaScript examples! You've now learned the basics of JavaScript, from variables and data types to operators, control structures, and functions. Keep practicing and exploring the countless possibilities JavaScript offers. Happy coding!