JavaScript Introduction
Understanding the world's most popular programming language
💻 What is JavaScript?
JavaScript is a programming language that makes web pages interactive. It can change content, respond to user actions, and create dynamic effects.
// This is JavaScript code
alert("Welcome to JavaScript!");
Result:
Shows a popup message saying "Welcome to JavaScript!"
What Can JavaScript Do?
Change Content
Modify text and HTML elements
document.getElementById("demo")
.innerHTML = "Hello World!";
Change Styles
Modify colors, sizes, and appearance
document.getElementById("demo")
.style.color = "red";
Calculate
Perform mathematical operations
let result = 10 + 5;
console.log(result); // 15
React to Events
Respond to clicks, typing, etc.
button.onclick = function() {
alert("Button clicked!");
};
🔹 JavaScript vs Other Languages
JavaScript is different from:
- HTML: HTML creates structure, JavaScript adds behavior
- CSS: CSS styles appearance, JavaScript creates interaction
- Java: Despite the name, they are completely different languages
<!-- HTML creates the button -->
<button id="myButton">Click Me</button>
<script>
// JavaScript makes it interactive
document.getElementById("myButton").onclick = function() {
alert("Hello from JavaScript!");
};
</script>
🔹 Simple JavaScript Examples
🔸 Display Current Time
// Get current time
let now = new Date();
let time = now.toLocaleTimeString();
console.log("Current time: " + time);
Console Output:
Current time: 2:30:45 PM
🔸 Simple Math
// Basic calculations
let x = 10;
let y = 5;
console.log("Addition: " + (x + y));
console.log("Multiplication: " + (x * y));
Console Output:
Addition: 15
Multiplication: 50
🔸 Interactive Example
// Ask user for their name
let userName = prompt("What's your name?");
alert("Nice to meet you, " + userName + "!");
🔹 Why JavaScript is Popular
- 🌐 Everywhere: Runs in all web browsers
- 📱 Versatile: Web, mobile, desktop, server applications
- 👥 Community: Huge community and resources
- 🚀 Easy Start: No installation needed
- 💼 Jobs: High demand in the job market