What is JavaScript?
-
JavaScript is a lightweight, interpreted scripting language used to make webpages interactive.
-
Created by Brendan Eich in 1995.
-
It runs in the browser (client-side) but can also run on servers (Node.js).
📌 Key Features of JavaScript
Feature | Description |
---|---|
Client-Side | Runs in the user's browser |
Interpreted | No need to compile |
Dynamic Typing | No need to declare variable type |
Object-Based | Uses objects for structure |
Event-Driven | Responds to user actions (click, type) |
Cross-Platform | Works on all browsers |
🧱 Basic Syntax
JavaScript is embedded within an HTML file using the <script>
tag.
📚 Basic Concepts with Examples
1. Variables
2. Data Types
3. Operators
🔁 Control Structures
If-else
Switch
Loops
🔤 Functions in JavaScript
Arrow Function:
🎯 Events in JavaScript
Event | Description |
---|---|
onclick | When element is clicked |
onchange | Value changed |
onload | When page loads |
onmouseover | Mouse over element |
Example:
📦 JavaScript and DOM (Document Object Model)
DOM allows JavaScript to interact with HTML elements.
⚙️ Arrays and Objects
Arrays
Objects
📐 Useful JavaScript Methods
Method | Use |
---|---|
alert() | Show popup message |
console.log() | Print to browser console |
prompt() | Input box popup |
parseInt() | Convert to integer |
toString() | Convert to string |
🌍 Practical Applications of JavaScript
Area | Examples |
---|---|
Web Interactivity | Click, hover, drag/drop |
Form Validation | Check required fields |
Animations | Interactive sliders, menus |
Web APIs | Google Maps, YouTube APIs |
Server-side (Node.js) | Real-time apps, chatbots |
❓ Frequently Asked MCQs
-
Who developed JavaScript?
a) Dennis Ritchie
b) Bjarne Stroustrup
c) Brendan Eich ✅
d) Tim Berners-Lee -
Which tag is used to write JavaScript in HTML?
a)<js>
b)<javascript>
c)<script>
✅
d)<code>
-
Which is NOT a JavaScript data type?
a) String
b) Number
c) Float ✅ (JS has onlyNumber
type)
d) Boolean -
Which method displays a message in a popup box?
a)print()
b)popup()
c)alert()
✅
d)show()
-
What is the result of
2 + "2"
in JavaScript?
a) 4
b) 22 ✅ (String concatenation)
c) NaN
d) Error
🧠 Important Points for Exams
-
JavaScript is case-sensitive
-
==
checks value only,===
checks value + type -
Can be used for both client-side and server-side
-
Commonly used with HTML + CSS
-
JavaScript files use
.js
extension
📝 Conclusion
JavaScript makes websites dynamic and interactive. It's essential for frontend development, and with Node.js, for backend as well.