JavaScript Foundations: Examples, Guide, Concepts

Learn the basics of JavaScript, including core syntax, variables, and data types. This guide gives beginners a clear path to see how JavaScript makes web pages interactive, from running simple scripts to using logical operators and functions.
authorImageVarun Saharawat27 May, 2026
JavaScript Foundations: Examples, Guide, Concepts

Many new developers feel overwhelmed when they first see a script. If you know HTML and CSS, making a page interactive can seem confusing. This article makes learning JavaScript Foundations easier by breaking down programming logic into simple steps. 

By focusing on the basics used in web development, you will learn to write clear, efficient code that makes your websites interactive.

Overview of JavaScript Foundations

JavaScript powers today’s web. While HTML sets up the structure and CSS handles the look, JavaScript controls how things behave. To start learning JavaScript, all you need is a web browser and a text editor. Many developers use the browser console to quickly test out small pieces of code.

To add JavaScript to your website, use the script tag. You can place your code directly in your HTML file or link to a separate file. Using an external file often keeps your project more organized and easier to manage.

How to connect JavaScript with HTML

  • There are two main ways to add scripts to your pages. One way is to write code directly between script tags in your HTML document.

  • The other way is to use external scripting, which links to a .js file using the src attribute. This method is preferred by most professionals.

JavaScript Foundations Syntax

The rules that govern how you write code are known as syntax. In JavaScript Foundations, syntax ensures the computer understands your instructions. JavaScript is case-sensitive, meaning that a variable named "User" is different from one named "user".

Every statement in JavaScript usually ends with a semicolon, though it is often optional in modern versions. However, using them is a best practice to avoid bugs during code compression. Comments are also vital; they allow you to leave notes in the code that the computer ignores.

Core Syntax Rules

  • Case Sensitivity: Variables and function names must be consistent in their casing.

  • Semicolons: Used to separate executable statements.

  • Comments: Use double slashes for single lines or a slash and asterisk for blocks of text.

JavaScript Foundations Variables

Variables act as containers for storing data values. In the early days, developers only used "var," but modern JavaScript Foundations introduces "let" and "const" for better data management. Understanding when to use each is a critical step for any beginner.

Most JavaScript statements end with a semicolon, although it’s often optional in newer versions. Still, using semicolons is a good habit to help prevent bugs when your code is compressed. Comments are also important because they let you leave notes in your code that the computer ignores.

The following table compares the three ways to declare variables in JavaScript:

Keyword

Scope

Reassignable

Description

var

Function

Yes

The older way to declare variables; generally avoided now.

let

Block

Yes

The standard for variables that need to change.

const

Block

No

Used for constants like API keys or fixed values.

JavaScript Foundations Concepts 

Data types define what kind of data a variable can hold. In JavaScript Foundations, you will work with several "primitive" types. These include Strings for text, Numbers for calculations, and Booleans for true or false logic.

You’ll also come across more complex types, such as Objects and Arrays. Arrays help you keep a list of items in one variable. Objects let you group related data using key-value pairs. Learning these will help you manage lots of information more easily.

Common Data Types

  • Strings: Text wrapped in quotes, such as "Hello World".

  • Numbers: Integers or decimals used for math.

  • Booleans: Logic gates that are either true or false.

  • Null and Undefined: Represent empty or unassigned values.

JavaScript Foundations Examples

The best way to learn is by seeing code in action. Here are some simple JavaScript Foundations examples that show how variables and operations work together. These short pieces of code perform a basic calculation and display a message to the user.

Let's start with a simple addition example. We assign numbers to variables, do the math, and then show the result in the browser console.

Basic Math Example

  • let price = 20;

  • let tax = 2;

  • let total = price + tax;

  • console.log(total); // Outputs 22

String Concatenation

  • let firstName = "John";

  • let greeting = "Hello, " + firstName;

  • console.log(greeting); // Outputs Hello, John

JavaScript Foundations Guide 

Operators let you do things with variables. In the JavaScript Foundations Guide, operators are grouped into categories like arithmetic, assignment, and comparison. You use these tools to build logic and make decisions in your code.

Comparison operators are important because they let your code compare two values. For example, you can check if a user's age is over 18 before letting them use certain features.

Types of Operators

  1. Arithmetic: Used for math (plus, minus, multiply, divide).

  2. Assignment: Used to give values to variables (equals sign).

  3. Comparison: Checks if values are equal or different (double or triple equals).

  4. Logical: Combines multiple conditions (AND, OR, NOT).

How to Start with JavaScript Foundations

Functions are blocks of code designed to perform a particular task. For anyone studying JavaScript Foundations for beginners, functions are the key to writing reusable code. Instead of writing the same logic five times, you write it once inside a function and "call" it whenever needed.

Conditional statements like "if-else" blocks let your code choose what to do based on different situations. For example, a website could show a "Log In" button when you are signed out, or a "Profile" button when you are signed in.

Creating a Simple Function

  • function sayHello(name) {

  • return "Welcome, " + name;

  • }

  • console.log(sayHello("Alex"));

Tips for JavaScript Foundations 

As you go through a JavaScript foundations tutorial, you will eventually learn about the Document Object Model, or DOM. The DOM links your JavaScript code to the HTML elements on your page. When you select an element, you can change its colour, text, or visibility based on what the user does.

Events are what trigger these changes. For example, a "click" event happens when someone presses a button. By using functions with event listeners, you can make buttons that open menus, submit forms, or start animations.

Handling User Interaction

  • Selection: Use methods such as getElementById to find an HTML tag.

  • Events: Add a listener to wait for a click or a keypress.

  • Modification: Change the style or content of the element once the event triggers.

FAQs

What is the difference between let and var in JavaScript?

"let" is block-scoped, meaning it only exists within the curly braces where it is defined, while "var" is function-scoped and can lead to unpredictable bugs.

How do I display data in the browser for debugging?

The most common way is using console.log(), which prints the value of a variable or a message into the browser's developer tools.

What are the three main components of JavaScript?

The core components are the ECMAScript language specification, the Document Object Model (DOM) for interacting with web pages, and the Browser Object Model (BOM).

Can JavaScript run outside of a web browser?

Yes, using environments like Node.js, JavaScript can be used for server-side development, desktop applications, and even robotics.

Is JavaScript the same as Java?

No, they are entirely different languages with different use cases; JavaScript is primarily for web interactivity, while Java is a compiled language often used for enterprise software.
Popup Close ImagePopup Open Image
Talk to a counsellorHave doubts? Our support team will be happy to assist you!
Popup Image
Join 15 Million students on the app today!
Point IconLive & recorded classes available at ease
Point IconDashboard for progress tracking
Point IconLakhs of practice questions
Download ButtonDownload Button
Banner Image
Banner Image