Featured Article

Simple JavaScript Code Examples for Beginners

Adam Jul 13, 2026

Unlocking the power of JavaScript often starts with understanding its fundamentals. Beginner-friendly and interactive, JavaScript is the programming language of the web, used to add interactivity and dynamic functionality to websites. Let's dive into simple JavaScript code examples that illustrate its core concepts.

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

JavaScript's simplicity allows new developers to start coding almost immediately. Its syntax is friendly and easy to understand, particularly for those familiar with English. With this in mind, let's explore two primary aspects of JavaScript: data types and basic operators.

HTML input types
HTML input types

Data Types and Basic Operators

In JavaScript, data is represented in various types, including numbers, strings, boolean values, null, undefined, object, and symbol. Each data type has its own characteristics and behavior.

Javascript Cheet cheet
Javascript Cheet cheet

Here are examples of the most common data types:

Data Type Example
Number let num = 10;
String let str = "Hello, World!";
Boolean let bool = true;
Null let nullVal = null;
Undefined let undef;
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet

Operators in JavaScript

Operators are symbols that perform operations on variables and values. They include arithmetic (+, -, *, /, %), assignment (=, +=, -=), comparison (==, ===, !=, !==), logical (&&, ||, !), and more.

Here's an example demonstrating the use of operators:

tg: webguild
tg: webguild

let x = 10;
let y = 5;

console.log(x + y);  // addition
console.log(x - y);  // subtraction
console.log(x * y);  // multiplication
console.log(x / y);  // division
console.log(x % y);  // modulus (remainder)

x += y;  // equivalent to x = x + y
console.log(x);  // 15

let isEqual = (x == y);  // loose comparison (type can be different)
console.log(isEqual);  // false

isEqual = (x === y);  // strict comparison (type AND value must match)
console.log(isEqual);  // false

Control Structures: If..Else and Loops

Control structures determine the flow of a program, guiding its output and behavior based on given input or conditions. JavaScript supports several control structures, with if..else statements and loops being the most common.

The if..else statement executes a block of code if a specified condition is true, and an alternate block if the condition is false. Here's an example:

Animation Login Form | Html&Css.
Animation Login Form | Html&Css.

let temp = 25;

if (temp > 30) {
  console.log("It's hot outside!");
} else if (temp > 20) {
  console.log("The weather is nice.");
} else {
  console.log("It's quite cold today.");
}

For Loop

The for loop repeats a block of code a specific number of times, making it ideal for iterating through arrays and other iterable objects. Here's an example that prints numbers 1 through 5:

login form html css
login form html css
an image of some type of computer screen with space in the background and stars above it
an image of some type of computer screen with space in the background and stars above it
a computer screen with an image of a key on it and the word's name in
a computer screen with an image of a key on it and the word's name in
The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had
10+ The Best javascript games code
10+ The Best javascript games code
JavaScript Basics | Chapter 1 ๐Ÿ“– | Part 1
JavaScript Basics | Chapter 1 ๐Ÿ“– | Part 1
an image of a screen with text on it that says, simple like button animation
an image of a screen with text on it that says, simple like button animation
A Simple Guide to ES6 Iterators in JavaScript with Examples
A Simple Guide to ES6 Iterators in JavaScript with Examples
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
Crรฉation d'un site CV monopage responsive en HTML et CSS - Pierre Giraud
Crรฉation d'un site CV monopage responsive en HTML et CSS - Pierre Giraud
JavaScript - The Event Loop
JavaScript - The Event Loop
8 Essential JavaScript Array Methods Visual Guide
8 Essential JavaScript Array Methods Visual Guide
JavaScript Cheat Sheet ๐Ÿš€ | Complete JS Reference for Beginners & Developers
JavaScript Cheat Sheet ๐Ÿš€ | Complete JS Reference for Beginners & Developers
the different types of web pages are shown
the different types of web pages are shown
an info sheet with different types of web pages
an info sheet with different types of web pages
JavaScript Basics | Chapter 1 ๐Ÿ“– | Part 2
JavaScript Basics | Chapter 1 ๐Ÿ“– | Part 2
๐Ÿš€ JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
๐Ÿš€ JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
How to Count Vowels in Java | Simple Code Example
How to Count Vowels in Java | Simple Code Example
an image of some type of webpage with dark colors and blue text on it
an image of some type of webpage with dark colors and blue text on it
an image of a computer screen with many lines and numbers on it, all in different colors
an image of a computer screen with many lines and numbers on it, all in different colors


for (let i = 1; i <= 5; i++) {
  console.log(i);
}

While Loop

The while loop repeatedly executes a block of code as long as a specified condition remains true. Here's an example that keeps prompting for user input until a valid number is entered:

let userInput;
do {
  userInput = prompt("Enter a number (1-10):");
} while (isNaN(userInput) || userInput < 1 || userInput > 10);

console.log("You entered: " + userInput);

With these simple examples, you now have a solid foundation for exploring more advanced JavaScript concepts. Happy coding!