Skip to main content
JavaScript Basics
CHAPTER 01 Beginner

Introduction to JavaScript

Updated: May 12, 2026
15 min read

# Introduction to JavaScript

Welcome to the world of JavaScript! Whether you are a student, a self-taught developer, or someone transitioning from HTML and CSS, this course is designed specifically for you. By the end of this journey, you will have a rock-solid understanding of JavaScript and how to build interactive, dynamic web applications.

1. Introduction

JavaScript is the programming language of the Web. While HTML defines the structure of a webpage and CSS handles the styling, JavaScript makes the webpage "alive" and interactive. It is one of the most popular and widely used programming languages in the world.

2. Learning Objectives

By the end of this chapter, you will be able to:

  • Understand what JavaScript is and why it is essential.
  • Learn the history and evolution of JavaScript.
  • Differentiate between Frontend and Backend JavaScript.
  • Write your very first JavaScript code to display a greeting.
  • Understand the basic use cases of JavaScript in modern web development.

3. What is JavaScript?

JavaScript (often abbreviated as JS) is a lightweight, interpreted, object-oriented programming language with first-class functions. Originally designed to make webpages interactive, it is now used everywhere—from front-end web development to back-end servers, mobile applications, and even game development.

Think of building a house:

  • HTML is the structure (walls, floors, doors).
  • CSS is the interior design (paint colors, styles, decorations).
  • JavaScript is the electricity and plumbing (making the lights turn on when you flip a switch, or water flow when you turn a tap).

4. History of JavaScript

JavaScript was created in 1995 by Brendan Eich while he was working at Netscape Communications. It was incredibly rushed and written in just 10 days! Initially, it was called *Mocha*, then *LiveScript*, and finally *JavaScript* (as a marketing tactic to ride the popularity wave of the Java programming language, though they are completely different languages).

Over the years, JavaScript has evolved massively. In 2015, a major update called ES6 (ECMAScript 2015) was released, which modernized the language and introduced many features we use today.

5. Why Learn JavaScript?

If you want to be a web developer, JavaScript is not optional—it is mandatory. Here is why:

  • It is the only language native to web browsers. Every browser (Chrome, Firefox, Safari) understands JavaScript.
  • Huge Community and Resources: Millions of developers use it, meaning finding help online is incredibly easy.
  • Versatile: You can build web apps, mobile apps, desktop apps, and servers using just JavaScript.
  • High Demand: JavaScript developers are among the most sought-after and well-paid professionals in the tech industry.

6. JavaScript Use Cases

Where is JavaScript actually used?

  • Adding interactivity to websites: Dropdown menus, image sliders, form validations.
  • Single Page Applications (SPAs): Fast, modern web apps like Gmail, Twitter, or Facebook (often built with frameworks like React, Vue, or Angular).
  • Back-end Servers: Using Node.js, JavaScript can run on servers to handle databases and user authentication.
  • Mobile Apps: Using frameworks like React Native, you can build iOS and Android apps with JS.
  • Browser Extensions: Ad-blockers, grammar checkers, and productivity tools.

7. Frontend vs Backend JavaScript

JavaScript used to be exclusively a "frontend" language. Let's clarify the difference:

Frontend JavaScript (Client-side)

This is JavaScript that runs inside the user's web browser. It manipulates the DOM (Document Object Model), handles user clicks, fetches data from the server, and updates the UI without reloading the page.

Backend JavaScript (Server-side)

This is JavaScript that runs on a server (usually using Node.js). It handles database operations, user authentication, file uploads, and API creations.

---

8. Real-world Examples & Code Snippets

Let's look at how JavaScript actually looks and behaves. Even if you don't understand all the syntax right now, focus on what the code is *doing*.

Example 1: Changing HTML Content

JavaScript can select an HTML element and change its content.

html
1234567891011
<!-- Example HTML -->
<p id="demo">Original Text</p>
<button onclick="changeText()">Click Me</button>

<script>
// Example JavaScript
function changeText() {
    // Select the element with id 'demo' and change its text
    document.getElementById("demo").innerHTML = "Hello JavaScript!";
}
</script>

Output Explanation: When the user clicks the button, the text inside the paragraph changes from "Original Text" to "Hello JavaScript!".

Example 2: Changing HTML Styles (CSS)

JavaScript can directly modify the CSS of an element.

html
123456789101112
<!-- Example HTML -->
<p id="colorText">Watch me change color!</p>
<button onclick="changeColor()">Change Color</button>

<script>
// Example JavaScript
function changeColor() {
    // Change the text color to red and increase font size
    document.getElementById("colorText").style.color = "red";
    document.getElementById("colorText").style.fontSize = "25px";
}
</script>

Output Explanation: Clicking the button instantly turns the text red and makes it larger.

Example 3: Hiding HTML Elements

JavaScript can hide elements on the screen.

html
1234567891011
<!-- Example HTML -->
<p id="hideMe">I am going to disappear!</p>
<button onclick="hideElement()">Hide</button>

<script>
// Example JavaScript
function hideElement() {
    // Set the display property to 'none'
    document.getElementById("hideMe").style.display = "none";
}
</script>

Output Explanation: The paragraph vanishes from the screen when the button is clicked.

Example 4: Showing Hidden HTML Elements

Conversely, JavaScript can reveal hidden elements.

html
1234567891011
<!-- Example HTML -->
<p id="showMe" style="display:none">Surprise! I am back!</p>
<button onclick="showElement()">Show</button>

<script>
// Example JavaScript
function showElement() {
    // Set the display property to 'block' to show it
    document.getElementById("showMe").style.display = "block";
}
</script>

Output Explanation: The hidden text appears when the button is clicked.

Example 5: Creating an Alert Box

You can pop up a message for the user.

html
12345678910
<!-- Example HTML -->
<button onclick="showAlert()">Alert Me!</button>

<script>
// Example JavaScript
function showAlert() {
    // Display a basic alert dialog
    alert("Welcome to the JavaScript course!");
}
</script>

Output Explanation: A native browser alert box pops up with the message.

Example 6: Writing into the HTML Output

You can write directly into the HTML document. *(Note: only use this for testing!)*

html
12345
<!-- Example HTML -->
<script>
// Example JavaScript
document.write(5 + 6); // Writes 11 directly to the page
</script>

Output Explanation: The number 11 is printed directly onto the webpage.

Example 7: Writing into the Browser Console

Developers use the console to test and debug code.

html
12345
<!-- Example HTML -->
<script>
// Example JavaScript
console.log("This message is hidden from users, but visible to developers!");
</script>

Output Explanation: Nothing changes on the web page. However, if you press F12 and open the "Console" tab, you will see this message.

Example 8: Basic Math Operations

JavaScript can act as a powerful calculator.

html
1234567891011
<!-- Example HTML -->
<p id="mathResult"></p>
<button onclick="doMath()">Calculate 10 * 5</button>

<script>
// Example JavaScript
function doMath() {
    let result = 10 * 5; // Multiplies 10 by 5
    document.getElementById("mathResult").innerHTML = "Result: " + result;
}
</script>

Output Explanation: Prints "Result: 50" on the screen.

Example 9: Swapping an Image

JavaScript can change the src attribute of an image.

html
1234567891011121314
<!-- Example HTML -->
<img id="myImage" src="bulboff.gif" width="100">
<button onclick="turnOn()">Turn On</button>
<button onclick="turnOff()">Turn Off</button>

<script>
// Example JavaScript
function turnOn() {
    document.getElementById(&#039;myImage').src = 'bulbon.gif';
}
function turnOff() {
    document.getElementById(&#039;myImage').src = 'bulboff.gif';
}
</script>

Output Explanation: Clicking "Turn On" changes the image source to a glowing bulb, simulating a light turning on.

Example 10: Simple Variables

Variables store data that can be used later.

javascript
1234
// Example JavaScript
let userName = "Alice";
let userAge = 25;
console.log(userName + " is " + userAge + " years old.");

Output Explanation: In the console, it outputs: "Alice is 25 years old."

---

9. Common Mistakes for Beginners

  1. 1. Confusing Java and JavaScript: They are entirely different languages. Java is heavily used for enterprise backends and Android apps; JavaScript is the king of the web.
  1. 2. Forgetting to include the <script> tags: If you write JS inside an HTML file, it *must* be wrapped inside <script> and </script>.
  1. 3. Case Sensitivity: JavaScript is strictly case-sensitive. getElementById works. getelementbyid will throw an error.

10. Best Practices

  • Write clean code: Use indentation and spaces to make your code readable.
  • Use Comments: Always add comments (// this is a comment) to explain what complex parts of your code do.
  • Check the Console: If your JavaScript isn't working, immediately open your browser's Developer Tools Console. The console will tell you exactly what the error is and on which line it occurred.

11. Mini Project: Dynamic Greeting Page

Let's build a small, interactive page combining HTML and JS.

html
1234567891011121314151617181920212223242526272829303132
<!-- Example HTML -->
<!DOCTYPE html>
<html>
<head>
    <title>Dynamic Greeting</title>
    <style>
        /* Example CSS */
        body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
        .btn { padding: 10px 20px; font-size: 16px; cursor: pointer; background: #007bff; color: white; border: none; border-radius: 5px; }
        .btn:hover { background: #0056b3; }
        #greeting { font-size: 24px; font-weight: bold; margin-top: 20px; color: #333; }
    </style>
</head>
<body>

    <h1>Welcome to JavaScript</h1>
    <button class="btn" onclick="generateGreeting()">Say Hello</button>
    <p id="greeting"></p>

    <script>
    // Example JavaScript
    function generateGreeting() {
        let name = prompt("What is your name?"); // Asks the user for input
        if(name) {
            document.getElementById("greeting").innerHTML = "Hello, " + name + "! Welcome to learning JavaScript!";
            document.getElementById("greeting").style.color = "green";
        }
    }
    </script>

</body>
</html>

How it works: When you click the button, a prompt asks for your name. It then displays a personalized, green greeting on the screen.

12. Exercises

  1. 1. Write a JavaScript line of code to show an alert box that says "I love coding!".
  1. 2. Create an HTML button that, when clicked, changes the text of an <h1> element from "Hello" to "Goodbye".
  1. 3. Write a JavaScript line to output the number 100 into the browser console.

13. MCQs (Multiple Choice Questions)

Q1: What does JavaScript do for a webpage? A) Styles the layout B) Defines the structure C) Adds interactivity and logic D) Hosts the website *Answer: C*

Q2: Is JavaScript the same as Java? A) Yes, JavaScript is a lighter version of Java. B) No, they are completely different languages. *Answer: B*

Q3: Which organization originally created JavaScript? A) Google B) Microsoft C) Netscape D) Apple *Answer: C*

14. Interview Questions

Q: Explain the difference between HTML, CSS, and JavaScript. *A: HTML is used for structure, CSS is used for styling and layout, and JavaScript is used to add interactivity, logic, and dynamic behavior to the webpage.*

Q: What is the DOM? *A: The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs (like JavaScript) can change the document structure, style, and content.*

15. FAQs

Q: Do I need to buy any software to learn JavaScript? *A: No! All you need is a text editor (like Notepad or VS Code) and a web browser (like Chrome). They are 100% free.*

Q: How long does it take to learn JavaScript? *A: You can learn the basics in a few weeks, but mastering it and its frameworks (like React or Node.js) can take months or years of practice.*

16. Summary

  • JavaScript brings webpages to life.
  • It was created in 1995 and has evolved into one of the world's most powerful languages.
  • You can use JS to change HTML content, manipulate CSS styles, hide/show elements, and calculate math.
  • JS is used on both the Frontend (Browser) and Backend (Servers).

17. Next Chapter Recommendation

Now that you know what JavaScript is and what it can do, it's time to set up your computer like a real developer. In the next chapter, Setting Up JavaScript Environment, we will learn how to write JS in Visual Studio Code and connect it properly to HTML files.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·