← Back to Chapters

JavaScript Introduction

⚡ JavaScript Introduction

? Quick Overview

JavaScript is a versatile and powerful programming language that runs in the browser (and on servers) to add interactivity and logic to web pages. With JavaScript, you can handle user actions, validate forms, animate elements, update content without reloading, and much more.

Think of HTML as the structure, CSS as the design, and JavaScript as the brain that makes everything respond and feel alive.

? Key Concepts

  • ✅ JavaScript runs in the browser and can also run on servers using environments like Node.js.
  • ✅ It is used to make web pages dynamic, interactive, and responsive to user actions.
  • ✅ JavaScript can manipulate HTML and CSS to change the content and styling on the fly.
  • ✅ It supports variables, functions, conditions, loops, objects, and much more.

? Where JavaScript Is Commonly Used

  • ? Web development (client-side and server-side)
  • ? Mobile app development (e.g., React Native)
  • ? Game development (browser-based and beyond)
  • ? Web servers and APIs (Node.js)
  • ? Desktop applications (Electron)

? Syntax and Theory

JavaScript syntax is the set of rules that define how JavaScript programs are written and understood by the browser.

  • ? Statements usually end with a semicolon ; (optional but recommended).
  • ? Code blocks are wrapped in curly braces { }, for example in functions and loops.
  • ? Comments help you describe your code: // single-line or /* multi-line */.
? View Basic JavaScript Syntax
// Single-line comment
/*
  Multi-line
  comment
*/
let name = "John";
console.log(name);

Here, let declares a variable, and console.log() prints its value to the browser's developer console.

? Code Examples

? Basic JavaScript Alert

This simple example shows JavaScript displaying a popup message when the page loads:

? View Alert Example
<script>
alert("Welcome to JavaScript!");
</script>

? Ways to Add JavaScript to HTML

There are three common ways to include JavaScript in an HTML page:

  • Inline – Directly inside an HTML element (e.g., using onclick).
  • Internal – Inside a <script> tag in the HTML file.
  • External – In a separate .js file linked to your HTML.

? Internal Script Example

? View Internal Script
<script>
console.log("Hello from internal script!");
</script>

? External Script Example

? View External Script
<script src="script.js"></script>

? Live Output / Explanation

JavaScript can produce output in different ways:

  • alert("Hello") – shows a popup alert box.
  • document.write("Text") – writes directly into the HTML page (mostly for simple demos).
  • console.log("Log") – prints a message to the browser's console (great for debugging).
  • innerHTML – updates the content inside an HTML element.

Example: using innerHTML to change the content of a paragraph:

? View innerHTML Example
<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>

? Interactive Demo

Click the button below to update this text using JavaScript.

? Tips & Best Practices

  • Start with the basics: variables, data types, operators, and functions.
  • Use console.log() often to see what your code is doing.
  • Keep your JavaScript in separate .js files for cleaner HTML.
  • Place scripts at the bottom of the HTML body or use defer so the page loads faster.
  • Practice manipulating the DOM (Document Object Model) using getElementById, querySelector, etc.

? Try It Yourself / Practice Tasks

  • Create a button that shows an alert saying "Hello, this is JavaScript!" when clicked.
  • Make a paragraph whose text changes when you click a button using innerHTML.
  • Log your name and age to the console using console.log().
  • Experiment by changing a variable value and printing it again to see the difference.

Example button with an inline JavaScript alert: