JavaScript
Introduction to JavaScript π
JavaScript (JS) is a lightweight, interpreted programming language used to create dynamic and interactive content on web pages. It is one of the core technologies of the web, along with HTML and CSS.
Why Use JavaScript?
β
Makes web pages interactive
β
Supports event handling (e.g., clicks, keypresses)
β
Works with HTML & CSS to enhance UI/UX
β
Can manipulate the DOM (Document Object Model)
β
Used in both frontend (browser) and backend (Node.js)
Basic JavaScript Syntax
Hereβs a simple JavaScript example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Example</title>
</head>
<body>
<h1 id=“demo”>Hello, JavaScript!</h1>
<button onclick=“changeText()”>Click Me</button>
<script>
function changeText() {
document.getElementById(“demo”).innerHTML = “You clicked the button!”;
}
</script>
</body>
</html>
Hello, JavaScript!
Ways to Add JavaScript
- Inline JavaScript (inside an HTML element)
<button onclick="alert('Hello!')">Click Me</button>
Internal JavaScript (inside a <script>
tag)
<script>
console.log("Hello from JavaScript!");
</script>
- External JavaScript (using a separate file)
<script src="script.js"></script>
Basic JavaScript Concepts
β Variables: Store data
let name = "John"; // Modern way (ES6) var age = 25; // Older way const PI = 3.14; // Constant
β Functions: Perform tasks
function greet() { alert("Welcome to JavaScript!"); }
β Events: React to user actions
<button onclick="greet()">Click Me</button>
β Conditionals: Control flow
if (age > 18) { console.log("You are an adult."); } else { console.log("You are a minor."); }
β Loops: Repeat actions
for (let i = 0; i < 5; i++) { console.log("Iteration " + i); }
Where is JavaScript Used?
π Web Development β Frontend (React, Vue, Angular)
β Backend Development β Node.js
π± Mobile Apps β React Native, Ionic
π₯ Game Development β Phaser.js
π€ Machine Learning β TensorFlow.js