iti
15 April 2025
Introduction to JavaScript π§ π»
JavaScript is a powerful, high-level programming language used to create dynamic and interactive content on websites. It works alongside HTML and CSS to enhance the functionality of web pages, making them more responsive and user-friendly. π
π What is JavaScript?
JavaScript is a client-side scripting language that runs directly in the userβs browser. It allows web developers to implement complex features such as:
- Interactive forms validation β
- Dynamic content updates without reloading the page π
- Animations and effects β¨
- Pop-ups, sliders, dropdowns and modals π±οΈ
π Key Features of JavaScript
- Lightweight: JavaScript code is compact and fast to load.
- Event-Driven: Executes based on user actions like clicks, hovers, or key presses.
- Platform Independent: Runs in all major web browsers (Chrome, Firefox, Safari, Edge, etc.)
- Object-Based: Uses objects to represent data and functions.
- Easy Integration: Can be easily embedded into HTML pages using the
<script>
tag.
π§Ύ Basic Syntax of JavaScript
<script>
// This is a comment
alert("Hello, World!"); // This shows a popup message
</script>
The alert()
function is commonly used to show messages to the user.
π Variables in JavaScript
Variables are used to store data. You can declare a variable using var
, let
, or const
:
var name = "John";
let age = 25;
const country = "India";
π JavaScript Control Structures
- if-else β Used for decision making
- for, while, do-while β Used for looping
if (age > 18) {
alert("You are eligible to vote");
} else {
alert("You are not eligible");
}
π§© Functions in JavaScript
function greet(name) {
alert("Hello, " + name);
}
greet("Amit");
Functions help reuse code and make programs modular and readable.
π JavaScript and HTML Interaction
JavaScript can interact with HTML elements using the Document Object Model (DOM). Example:
<button onclick="showMessage()">Click Me</button>
<script>
function showMessage() {
alert("You clicked the button!");
}
</script>
π Uses of JavaScript in Real-world Applications
- Form validations on websites π
- Online