Notes – Introduction to JavaScript
What is JavaScript?
JavaScript (JS) is a high-level, interpreted programming language used to make web pages interactive. It is one of the core technologies of web development, along with HTML and CSS.
Key Features of JavaScript
- Lightweight โ Runs inside the browser without the need for extra software.
- Interpreted โ No need for compilation, the browser executes the code directly.
- Event-driven โ Can handle user interactions like clicks, form submissions, and key presses.
- Dynamic โ Allows modifying HTML and CSS in real-time.
- Cross-platform โ Works on all major browsers and operating systems.
Why is JavaScript Important?
JavaScript is used everywhere in modern web development. It helps create:
- Dynamic websites โ Interactive content, animations, and real-time updates.
- Web applications โ Forms, chats, notifications, and dashboards.
- Mobile applications โ With frameworks like React Native.
- Server-side applications โ Using Node.js.
How JavaScript Works in the Browser
- The browser downloads the web page (HTML, CSS, JS files).
- The JavaScript engine inside the browser reads and executes the JS code.
- JS can manipulate the webpage elements and handle user interactions.
JavaScript Execution in a Webpage
JavaScript can be written in two ways:
- Inline JavaScript (Inside HTML elements)
<button onclick="alert('Hello, JavaScript!')">Click Me</button> - External JavaScript File
<script src="script.js"></script>
This keeps the HTML clean and separates logic from structure.
Basic JavaScript Syntax
Here are a few simple JavaScript concepts:
| Concept | Example |
|---|---|
| Variables | let name = "John"; |
| Functions | function greet() { alert("Hello!"); } |
| Events | document.getElementById("btn").onclick = greet; |
| Loops | for(let i = 0; i < 5; i++) { console.log(i); } |
| Conditions | if (age > 18) { console.log("Adult"); } |
How to Run JavaScript?
You can execute JavaScript using:
- Browser Console โ Open Developer Tools (
F12in Chrome) โ Console โ Type JS Code. - Inside an HTML file โ Use
<script>tags within an HTML document. - External JS file โ Write JavaScript in a
.jsfile and link it to HTML.
