Essential Basic Javascript Code Examples For Beginners

Bobby Jul 13, 2026

Mainstream application of JavaScript starts off with a profound knowledge of its fundamental elements. As a web developer, mastering JavaScript basics forms a crucial part of your technical skillset. Let's explore some of these essentials with simple yet effective code examples.

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

By the end of this article, you'll have a solid grasp of JavaScript basics, making you well-poised to embark on more complex projects in the future.

HTML input types
HTML input types

JavaScript Basics: Variables and Data Types

Familiarizing oneself with JavaScript's variable declaration and data types is a fundamental aspect of coding. Let's take an example:

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

```javascript let message = "Hello, World!"; console.log(message); ```

In this snippet, `message` is a variable of string data type. The `console.log()` function logs the content of `message` in the developer console.

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

Numbers

JavaScript can perform mathematical operations using the number data type. Here's an example:

```javascript let num1 = 10; let num2 = 5; let sum = num1 + num2; console.log(sum); ```

JavaScript
JavaScript

This code will output `15`, the sum of `num1` and `num2`.

Strings

String manipulation is another skill that is important in JavaScript. Consider this example:

How to Create a Function in JavaScript: Step-by-Step Guide 🚀
How to Create a Function in JavaScript: Step-by-Step Guide 🚀

```javascript let str1 = "JavaScript"; let str2 = " is awesome!"; let combined = str1.concat(str2); console.log(combined); ```

The above JavaScript code will output `JavaScript is awesome!

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had
Javascript Cheet cheet
Javascript Cheet cheet
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
the operator's manual for programming
the operator's manual for programming
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 String Methods
Javascript String Methods
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
Coding with Parvez
Coding with Parvez
a computer screen with an image of a person flying through the air in front of stars
a computer screen with an image of a person flying through the air in front of stars
JavaScript Cheat Sheet 🚀 | Complete JS Reference for Beginners & Developers
JavaScript Cheat Sheet 🚀 | Complete JS Reference for Beginners & Developers
JavaScript Conditional Operators: Write Smarter Code with ? : 🚀
JavaScript Conditional Operators: Write Smarter Code with ? : 🚀
the different types of html tags are shown in this screenshote screen graber
the different types of html tags are shown in this screenshote screen graber
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
the top 10 javasecript projects for beginners to do list application calculator
the top 10 javasecript projects for beginners to do list application calculator
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
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
Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
Functions in Javascript
Functions in Javascript
Javascript Loops & Iterations
Javascript Loops & Iterations

Control Structures: Conditionals and Loops

JavaScript includes control structures to direct the flow of your program. They are critical for functions, statements, and many other elements in JavaScript.

Here's a demonstration of a simple if-else statement:

If-Else Statement

```javascript let score = 85; if (score >= 90) { console.log('Grade: A'); } else if (score >= 80) { console.log('Grade: B'); } else { console.log('Grade: C'); } ```

In this code, the output will be 'Grade: B' since the given score (85) falls in the B category.

Loops

Loops are essential for repetitive tasks. Here is an example of a `for` loop:

```javascript for (let i = 0; i < 5; i++) { console.log(i); } ```

This code will print numbers from 0 to 4 inclusive.

JavaScript basics can seem daunting at first, but with practice and the right examples, you'll find they quickly become second nature. Keep honing your skills, and in no time, you'll be writing more advanced JavaScript code. Happy coding!