JavaScript Tutorial

Introduction

JavaScript plays a crucial role in creating a dynamic website.

JavaScript is one of the most popular programming languages in the world and is a core technology of web development alongside HTML and CSS. Whether you’re browsing social media, shopping online, or watching videos, JavaScript is working behind the scenes to enhance your experience.

What is JavaScript?

JavaScript is a high-level, interpreted programming language primarily used to create dynamic and interactive content on websites. It allows developers to implement complex features such as:

  1. Interactive forms
  2. Animations
  3. Real-time updates
  4. Dynamic content rendering

JavaScript was developed by Brendan Eich in 1995. JavaScript has evolved significantly and is now used not only in browsers but also on servers (using Node.js), mobile apps, and even game development.

Initially, JavaScript was used for web browsers to build attractive web pages.
After Node came, it is used for Web and mobile apps, real-time networking applications like real chat, etc.

📖
Important:
  • Lightweight and fast.
  • Platform-independent.
  • Event-driven.
  • Supports object-oriented programming.
  • Runs directly in the browser.

Where does JavaScript run?

JavaScript code is written into the script tag.

Syntax:


<script>
// write code

</script>

Write the code into the script tag


<script>
document.write("Hello JavaScript");
</script>
Try it Yourself

Output:

Hello JavaScript

2) You can run JavaScript in the browser console.


console.log("Hello JavaScript");

Output:

Hello JavaScript

Add two numeric values


<script>
document.write(2+2);
</script>
Try it Yourself

Output:

4

Add two strings


<script>
document.write("Welcome" + " John");
</script>

Try it Yourself

Output:

Welcome John

Why is JavaScript Used?

1. Create Interactive Websites

JavaScript allows developers to respond to user actions like clicks, typing, scrolling, etc.

2. Client-Side Validation

It validates user input before sending data to the server, improving performance and user experience.

3. Dynamic Content Updates

Content can be updated without reloading the page (using AJAX or Fetch API).

4. Build Web Applications

Modern frameworks like React, Angular, and Vue are built using JavaScript.

5. Server-Side Development

With Node.js, JavaScript can also run on the server.

6. Cross-Platform Development

JavaScript is used for mobile apps (React Native) and desktop apps (Electron).

JavaScript Syntax

JavaScript syntax refers to the set of rules that define how JavaScript programs are written.

📖
Basic Syntax Rules:
  • Statements end with a semicolon (;) (optional but recommended)
  • Variables are declared using var, let, or const.
  • Case-sensitive language.
  • Supports object-oriented programming.
  • Uses curly braces {} for blocks.

Example:


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

Output:

Hello, World!

Example of JavaScript

Let’s look at a simple example that changes text when a button is clicked.

Use of HTML + JavaScript:


<!DOCTYPE html>
<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>

<h2 id="text">Welcome!</h2>
<button onclick="changeText()">Click Me</button>

<script>
function changeText() {
   document.getElementById("text").innerHTML = "Hello, JavaScript!";
}
</script>

</body>
</html>

Explanation:

  1. A button is created
  2. When clicked, it calls the changeText() function
  3. The function updates the content of the <h2> element

Common Mistakes in JavaScript

Beginners should avoid these below error.

1. Using == Instead of ===


<script>
document.write("Hello JavaScript");
</script>

Note: Always prefer === for strict comparison.

2. Forgetting let or const

Always declare variables properly.


x = 10; // bad practice

3. Not Handling Errors

Please use try and catch for the code.


try {
   // risky code
} catch (error) {
   console.log(error);
}

Interview Questions

Q 1: What is JavaScript?
Ans: JavaScript is a client-side scripting language used to create interactive and dynamic web pages.
Q 2: Is JavaScript compiled or interpreted?
Ans: JavaScript is an interpreted language, executed by the browser’s JavaScript engine.
Q 3: Where is JavaScript used?
Ans: It is used in web development, mobile apps, server-side (Node.js), and game development.
Q 4: Can JavaScript run without a browser?
Ans: Yes, using environments like Node.js.
Q 5: Who developed JavaScript?
Ans: Brendan Eich developed JavaScript in 1995.

Conclusion

JavaScript is an essential programming language for modern web development. It transforms static web pages into interactive and dynamic applications. From simple form validation to building complex web apps, JavaScript plays a vital role in enhancing user experience.

If you’re starting your journey in web development, learning JavaScript is a must. With consistent practice and understanding of core concepts, you can build powerful applications and open doors to numerous career opportunities.

JavaScript Tutorial – Objective Questions (MCQs)

Q1. JavaScript is a ______ language.






Q2. JavaScript is mainly used to ______.






Q3. JavaScript runs on the ______ side by default.






Q4. Which of the following is a JavaScript engine?






Q5. JavaScript was created by ______.