JavaScript Introduction

JavaScript Introduction

Introduction to JavaScript 

JavaScript (JS) is a dynamic, high-level, and interpreted programming language that adds interactivity to web pages. It is widely used for frontend and backend development.

1️⃣ What Can JavaScript Do?

Manipulate HTML & CSS – Update page content dynamically.
Handle Events – Respond to user actions like clicks or keypresses.
Validate Forms – Ensure correct user input before submitting data.
Work with APIs – Fetch and display data from servers.
Create Animations – Add interactive motion to elements.
Build Web Apps – Full-fledged applications with frameworks like React, Vue, and Angular.

2️⃣ Adding JavaScript to HTML

Inline JavaScript (not recommended)

<button onclick="alert('Hello, World!')">Click me</button>

Internal JavaScript (inside <script> tags)

<!DOCTYPE html> <html> <head> <title>JavaScript Example</title> </head> <body> <button id="btn">Click me</button> <script> document.getElementById("btn").addEventListener("click", function() { alert("Hello, World!"); }); </script> </body> </html>

External JavaScript (Best Practice)

Save the JavaScript in a separate file (script.js) and link it in HTML.

<script src="script.js"></script>

🔹 Benefit: Keeps HTML clean and allows code reuse.

3️⃣ Variables in JavaScript

JavaScript uses three keywords for declaring variables:

KeywordScopeCan Reassign?Can Redeclare?
varFunction-scoped✅ Yes✅ Yes
letBlock-scoped✅ Yes❌ No
constBlock-scoped❌ No❌ No

✅ Example:

var x = 10; // Global let y = 20; // Block-scoped const z = 30; // Cannot be changed y = 25; // Allowed // z = 35; ❌ ERROR: Cannot reassign const

4️⃣ Data Types in JavaScript

TypeExample
String"Hello" or 'World'
Number42, 3.14
Booleantrue, false
Array[1, 2, 3]
Object{name: "John", age: 25}
Undefinedlet a; (value not assigned)
Nulllet b = null; (empty value)

🔹 typeof checks a variable’s type:

console.log(typeof "Hello"); // "string" console.log(typeof 42); // "number" console.log(typeof true); // "boolean" console.log(typeof {}); // "object" console.log(typeof []); // "object" (Arrays are objects) console.log(typeof undefined); // "undefined" console.log(typeof null); // "object" (JavaScript bug)

5️⃣ Operators in JavaScript

OperatorExampleDescription
+5 + 3Addition
-5 - 3Subtraction
*5 * 3Multiplication
/5 / 3Division
%5 % 3Modulus (Remainder)
++x++Increment
--x--Decrement

Comparison Operators

OperatorExampleDescription
==5 == "5"Equal (loose)
===5 === "5"Equal (strict, checks type)
!=5 != "5"Not equal
!==5 !== "5"Not equal (strict)
>5 > 3Greater than
<5 < 3Less than

6️⃣ Control Structures (if-else, loops)

If-else statement

let age = 18; if (age >= 18) { console.log("You are an adult."); } else { console.log("You are a minor."); }

For Loop

for (let i = 0; i < 5; i++) { console.log("Iteration " + i); }

While Loop

let count = 0; while (count < 5) { console.log("Count: " + count); count++; }

7️⃣ Functions in JavaScript

Regular Function

function greet(name) { return "Hello, " + name + "!"; } console.log(greet("Alice"));

Arrow Function (ES6)

const greet = (name) => `Hello, ${name}!`; console.log(greet("Alice"));

8️⃣ Arrays & Objects

Arrays

let fruits = ["Apple", "Banana", "Cherry"]; console.log(fruits[0]); // "Apple" fruits.push("Orange"); // Add new item console.log(fruits.length); // 4

Objects

let person = { name: "Alice", age: 25, city: "New York" }; console.log(person.name); // "Alice" console.log(person["age"]); // 25

9️⃣ Event Handling (Click Event Example)

<button id="myBtn">Click me</button> <script> document.getElementById("myBtn").addEventListener("click", function() { alert("Button Clicked!"); }); </script>

🔟 JavaScript in the Browser

document.getElementById("id") – Selects an element by ID.
document.querySelector(".class") – Selects the first element with the class.
document.createElement("div") – Creates a new HTML element.
element.innerHTML – Sets or gets HTML content.
element.style.color = "red" – Changes an element’s CSS.

🔹 Summary

JavaScript is used for dynamic, interactive web pages.
Variables are declared with var, let, or const.
Data types include strings, numbers, booleans, arrays, and objects.
Operators perform arithmetic and logical operations.
Control structures (if, loops) control program flow.
Functions perform reusable actions.
Events handle user interactions.

🚀 Want to explore more? Let me know!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close