Opens in a new tab
Software Training Institute in Chennai with 100% Placements – SLA Institute
Share on your Social Media

JavaScript Tutorial for Beginners

Published On: October 26, 2026

Introduction

JavaScript is a core technology for building interactive websites and web applications. If you are just starting to learn to program, this JavaScript Tutorial for Beginners provides clear explanations and easy examples. The tutorial covers how JavaScript works, how to write a program, and how to use variables, data types, operators, loops, functions, arrays, and objects. It also introduces webpage interaction through the Document Object Model (DOM). These fundamentals provide a foundation for understanding web development and building useful programming skills.

What Is JavaScript?

JavaScript is a programming language that adds behavior and interactivity to websites. HTML defines webpage structure, CSS controls presentation, and JavaScript enables dynamic features such as responding to button clicks, updating content, processing information, and validating form inputs.

JavaScript is widely used in frontend development and can also run on servers through environments such as Node.js. This flexibility makes it useful for building websites, web applications, interactive tools, and various software solutions.

Why JavaScript Is Used

JavaScript supports common web development tasks:

  • Interactive websites: Responding to user actions and updating page elements.
  • Form validation: Checking entered information before processing a form.
  • Dynamic content: Displaying updated information based on application data.
  • Web applications: Supporting dashboards, shopping platforms, and learning portals.
  • Server-side development: Running application logic outside the browser with Node.js.

For example, an online shopping website can use JavaScript to update a shopping cart total whenever the product quantity changes. A learning platform can display quiz results after submitting answers.

How JavaScript Works in a Browser

A browser processes HTML to understand webpage structure and CSS to apply styles. JavaScript then executes instructions that can respond to events, calculate values, and interact with webpage elements through browser APIs.

JavaScript can be added directly to an HTML document using a script element:

<script>

  console.log(“Welcome to JavaScript!”);

</script>

The browser runs the statement and shows the message in the developer console. This simple example shows how JavaScript commands create results.

How to Get Started with JavaScript

Getting started with JavaScript requires only a modern web browser and a text editor. Beginner programs can be created without installing a complex development environment, making the language accessible to new programmers.

Using Browser Developer Tools

Browser developer tools include a JavaScript console for entering expressions, testing programs, and examining output. In desktop browsers, developer tools can be opened using F12, while Ctrl + Shift + J opens the console in Chrome on many Windows and Linux setups.

The following statement can be entered directly into the console:

console.log(“My JavaScript program”);

Output:

My first JavaScript program

The console.log() method displays information in the console. It is useful for learning how statements execute and for inspecting values during debugging.

Writing Your First JavaScript Program

An HTML file provides another way to practice JavaScript. The following example can be saved as index.html. Open it in a browser.

<!DOCTYPE html>

<html lang=”en”>

<head>

  <meta charset=”UTF-8″>

  <title>First JavaScript Program</title>

</head>

<body>

  <h1>Welcome to JavaScript</h1>

  <script>

    const learnerName = “Alex”;

    console.log(`Hello, ${learnerName}!`);

  </script>

</body>

</html>

The webpage displays a heading, and the console shows Hello, Alex!. The example introduces a variable, a string, and a template literal. Template literals use backticks and allow expressions to be embedded inside text with ${}.

JavaScript Variables and Data Types

Variables store information that programs can access and process. Data types are the kinds of values being handled, helping JavaScript perform operations according to the value involved.

Understanding let, const, and var

JavaScript offers three common variable declaration keywords: let, const, and var.

const courseName = “JavaScript”;

let lessonsCompleted = 3;

lessonsCompleted = 4;

console.log(courseName);

console.log(lessonsCompleted);

In this example, courseName holds the course name, and lessonsCompleted tracks progress. The let keyword allows reassignment, whereas const prevents variable reassignment.

Modern JavaScript usually prefers const when reassignment is not needed and prefers let when a variable must hold values. The var keyword remains relevant when studying code, although var’s function-scoped behavior differs from the block-scoped behavior of let and const.

A const declaration does not make an object or array immutable. Properties and elements remain changeable without additional protection.

Common JavaScript Data Types

JavaScript has kinds of data types that help us store and use information. Knowing these data types makes it easier to keep values organized and do operations in a program.

  • String: Represents text, such as “Hello” or “JavaScript”.
  • Number: Represents numeric values, such as 25, 10.5, or -3.
  • Boolean: Represents logical values, either true or false.
  • Undefined: Represents a variable that has been declared but has not been assigned a value.
  • Null: Represents the intentional absence of a value.
  • Array: Stores an ordered collection of values, such as [“HTML”, “CSS”, “JavaScript”].
  • Object: Groups related information using key-value pairs, such as { name: “Alex”, age: 21 }.

A practical example demonstrates how different values can be stored and displayed:

const learnerName = “Alex”;

const learnerAge = 21;

const courseActive = true;

console.log(learnerName);

console.log(learnerAge);

console.log(courseActive);

Using the correct data types makes code easier to read. It also helps developers keep information organized.

JavaScript Operators and Conditional Statements

Operators perform calculations, compare values, give data, and join expressions. Conditional statements use these expressions to decide which instructions to run.

Working with Operators

JavaScript includes several categories of operators:

  • Arithmetic operators: +, -, *, /, and % perform mathematical operations.
  • Comparison operators: >, <, >=, <=, ===, and !== compare values.
  • Logical operators: &&, ||, and ! combine or reverse logical conditions.
  • Assignment operators: =, +=, and -= assign or update values.

Consider a shopping calculation:

const price = 500;

const quantity = 2;

const total = price * quantity;

console.log(total); // 1000

The multiplication operator figures out the total cost. The strict equality operator, ===, checks both value and type, which is good for exact matches.

Using if…else Statements

Conditional statements let programs run different instructions based on whether a condition is true.

const score = 85;

if (score >= 50) {

  console.log(“Learning goal achieved”);

} else {

  console.log(“Continue practicing”);

}

Because the score is higher than or equal to 50, the first message shows up in the console.

An if…else Statement is helpful in quiz apps, registration forms, shopping systems, and learning sites. The else if statement allows programs to choose between multiple outcomes based on conditions.

JavaScript Loops and Functions

Loops and functions help organize code, repeat operations, and reuse logic. These concepts are essential for developing programs that are easier to maintain and extend.

Understanding Loops

Loops repeat a block of code according to a condition or iteration rule. Common JavaScript loops include for, while, and for…of.

A for loop can display numbers from one to five:

for (let number = 1; number <= 5; number++) {

  console.log(number);

}

The loop starts with number = 1, checks whether number <= 5 before each iteration, and increments the number by one after each iteration. It prints the numbers 1, 2, 3, 4, and 5.

Loops are useful for:

  • Displaying items from a list.
  • Calculating totals from collections of values.
  • Processing records in an application.
  • Repeating operations a specified number of times.

The choice of loop depends on the task. A for…of loop is particularly useful for iterating over values, such as arrays.

Creating and Calling Functions

A function is a block of code that performs a specific task. Functions can accept parameters, process information, and return results.

function calculateTotal(price, quantity) {

  return price * quantity;

}

const total = calculateTotal(250, 3);

console.log(total); // 750

The calculateTotal function accepts two parameters, multiplies them, and returns the result. Calling the function with 250 and 3 produces 750.

Functions help divide a program into manageable parts. They can handle calculations, process form input, generate messages, and perform repeated operations without duplicating the logic throughout a program.

JavaScript Arrays, Objects, and DOM Manipulation

Arrays, objects, and the Document Object Model (DOM) allow JavaScript to organize information and interact with webpage elements. These features provide a foundation for creating websites and practical browser-based applications.

Working with Arrays and Objects

An array stores an ordered collection of values, while an object groups related information using properties.

const topics = [“Variables”, “Functions”, “Arrays”];

console.log(topics[0]); // Variables

const learner = {

  name: “Alex”,

  course: “JavaScript”,

  progress: 75

};

console.log(learner.name); // Alex

console.log(learner.progress); // 75

Array indexing starts at zero, so topics[0] retrieves the first element. Object properties can be accessed through dot notation, such as learner.name.

Arrays are useful for managing lists of lessons, products, and tasks. Objects are useful for representing information about learners’ orders or application settings.

Changing Webpage Content with the DOM

The DOM represents an HTML document as a structure of objects that JavaScript can access and modify. For example, JavaScript can change a heading when a button is clicked.

<h2 id=”message”>Start learning JavaScript</h2>

<button id=”updateButton”>Show Message</button>

<script>

  const message = document.querySelector(“#message”);

  const button = document.querySelector(“#updateButton”);

  button.addEventListener(“click”, function () {

    message.textContent = “Keep practicing JavaScript!”;

  });

</script>

When the button is clicked, the heading changes. The querySelector() method selects an element using a CSS selector, while addEventListener() registers a function to respond to an event.

This example demonstrates how JavaScript connects program logic with webpage content.

Handling User Events

Events represent actions or changes that occur in a browser, including clicks, keyboard input, and form submissions. JavaScript event handlers respond to these actions and make webpages interactive.

Common applications include:

  • Updating a counter when a button is clicked.
  • Displaying a confirmation message after a form submission.
  • Changing displayed content when an option is selected.
  • Responding to text entered into a search field.

Combining events with functions, arrays, objects, and DOM methods makes it possible to build practical projects such as to-do lists, quiz applications, interactive calculators, and simple dashboards. These projects reinforce programming concepts through hands-on practice.

Conclusion

Learning JavaScript gets easier when you practice regularly and understand the basics well. This JavaScript Tutorial for Beginners covers variables, data types, operators, conditions, loops, functions, arrays, objects, and DOM manipulation. Running the examples and building small projects strengthens these concepts. With practice, the foundation makes it easier to learn advanced topics like asynchronous programming, APIs, and modern web application development. 

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.