Explanation of JavaScript Definition
1️⃣ “JavaScript is a lightweight, interpreted programming language.”
Lightweight → JavaScript does not need heavy software or high system resources.
Interpreted → It runs line by line directly in the browser without compiling.
👉 This makes JavaScript fast and easy to use.
2️⃣ “It is designed for creating network-centric applications.”
Network-centric means applications that work on the internet.
JavaScript is mainly used in websites and web applications that interact with servers.
👉 Example: Online forms, chat apps, live notifications.
3️⃣ “It is complimentary to and integrated with Java.”
Complimentary means JavaScript works alongside Java, but it is not the same as Java.
Integrated means JavaScript can be used in web pages where Java technologies are also used.
👉 Java is mostly used on the server side, JavaScript on the client side (browser).
4️⃣ “JavaScript is very easy to implement because it is integrated with HTML.”
JavaScript code can be written inside HTML files.
No extra setup is required.
👉 Example:
<script>
alert("Hello World");
</script>
5️⃣ “It is open and cross-platform.”
Open → Free to use, no license required.
Cross-platform → Works on Windows, Linux, macOS, Android, etc.
👉 Any device with a browser can run JavaScript.
6️⃣ “This tutorial has been prepared for JavaScript beginners…”
The tutorial is made for new learners.
It explains basic concepts step by step.
7️⃣ “…to build dynamic web pages and web applications.”
Dynamic web pages → Pages that change without reloading.
JavaScript adds interactivity.
👉 Examples:
Button click actions
Form validation
Image sliders
Live data updates
Short Summary
JavaScript is a lightweight and interpreted programming language used for developing interactive and dynamic web pages. It works inside the browser, is easy to integrate with HTML, supports internet-based applications, and runs on all platforms.
Features of JavaScript Explanation
1️⃣ JavaScript is a lightweight, interpreted programming language
JavaScript does not need much memory or system resources.
It is interpreted, so it runs directly in the browser without compiling.
👉 This makes JavaScript fast and easy to use.
2️⃣ Designed for creating network-centric applications
JavaScript is mainly used for internet-based applications.
It helps web pages communicate with servers.
👉 Example: Online forms, chat applications, live data updates.
3️⃣ Complementary to and integrated with Java
JavaScript works along with Java, but it is different from Java.
Java is mostly used on the server side, while JavaScript runs in the browser.
👉 They support each other in web development.
4️⃣ Complementary to and integrated with HTML
JavaScript is written inside HTML pages.
It adds interactivity to static HTML pages.
👉 Example: Button click, form validation, pop-ups.
5️⃣ It is a case-sensitive language
Uppercase and lowercase letters are treated differently.
name, Name, and NAME are not the same.
👉 Programmers must write code carefully.
6️⃣ JavaScript is supportable in operating system
JavaScript works on all operating systems.
It runs on any device that has a web browser.
👉 Example: Windows, Linux, macOS, Android.
7️⃣ It provides good control to the users over the web browsers
JavaScript can control browser actions.
It can handle events like click, scroll, load, and input.
👉 Example:
Show alerts
Validate forms
Change webpage content dynamically
Summary
JavaScript is a lightweight, interpreted, and case-sensitive scripting language used for creating dynamic and network-centric web applications. It is integrated with HTML and Java, works on all operating systems, and provides strong control over web browser behavior.
Uses of JavaScript Explanation
1️⃣ Client-side validation
JavaScript checks user input before sending data to the server.
This saves time and reduces server load.
👉 Example: Checking empty fields, email format, password length.
2️⃣ Dynamic drop-down menus
JavaScript changes menu options based on user selection.
Makes websites more interactive.
👉 Example: Selecting country → shows related states/cities.
3️⃣ Displaying date and time
JavaScript can show the current date and time automatically.
Useful for dashboards and websites.
👉 Example: Showing today’s date on a webpage.
4️⃣ Displaying pop-up windows and dialog boxes
JavaScript can show alerts, confirmations, and prompts.
Used to show messages to users.
👉 Example: “Are you sure you want to delete?”
5️⃣ Displaying clocks
JavaScript can create digital clocks that update every second.
👉 Example: Live clock on a website header.
6️⃣ Event handling
JavaScript reacts to user actions like click, hover, scroll, key press.
Makes the website responsive.
👉 Example: Button click performing an action.
7️⃣ Developing mobile applications
JavaScript is used with frameworks like React Native and Ionic.
Helps create mobile apps for Android and iOS.
8️⃣ Creating web browser–based games
JavaScript is used to develop interactive games in the browser.
Works with HTML5 Canvas.
👉 Example: Puzzle games, racing games.
9️⃣ Building web servers
JavaScript can run on servers using Node.js.
Used to build fast and scalable backend applications.
👉 Example: APIs, real-time chat servers.
🔟 Adding interactivity to websites
JavaScript makes web pages dynamic and interactive.
Without JavaScript, websites are mostly static.
👉 Example: Image sliders, animations, form validation.
Summary
JavaScript is used for client-side validation, dynamic menus, displaying date and time, pop-up messages, event handling, mobile app development, browser-based games, web servers, and adding interactivity to websites.
Ways to Add JavaScript in HTML
JavaScript code can be added to an HTML page in three ways:
1️⃣ JavaScript in <body> section
JavaScript code is written inside <script> tag in the body.
It runs when the page loads.
Example
<!DOCTYPE html>
<html>
<body>
<script>
document.write("Welcome to JavaScript");
</script>
<p>This example shows JavaScript inside body section.</p>
</body>
</html>
2️⃣ Inline JavaScript
JavaScript is written directly inside HTML tags.
Mostly used with events like onclick, onmouseover.
Example
<!DOCTYPE html>
<html>
<body>
<button onclick="alert('Hello Class 12')">Click Here</button>
</body>
</html>
3️⃣ External JavaScript File
JavaScript code is written in a separate .js file.
Included in HTML using <script src="">.
HTML File
<html>
<body>
<button onclick="display()">Result</button>
<script src="hello.js"></script>
</body>
</html>
JavaScript File (hello.js)
function display() {
alert("Hello World!");
}
✅ Advantages:
Code reuse
Easy maintenance
Clean HTML file
2. JavaScript Comments
Comments are used to explain code.
They are ignored by the browser.
1️⃣ Single-line Comment (//)
<script>
var a = 10; // variable a
document.write(a);
</script>
2️⃣ Multi-line Comment (/* */)
<script>
/* This is
a multi-line
comment */
document.write("Hello JavaScript");
</script>
3. Display Functions in JavaScript
1️⃣ document.write()
Used to display output on the webpage.
<script>
document.write(7 + 5);
</script>
2️⃣ alert() (Window Alert)
Used to show popup message.
<script>
alert("Welcome to JavaScript");
</script>
3️⃣ console.log()
Used to display output in browser console (for debugging).
<script>
console.log("Gurukul College");
console.log(7 + 5);
</script>
4️⃣ innerHTML
Used to change content inside an HTML element.
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
4. JavaScript Data Types
JavaScript is a dynamic typed language, so we don’t declare data type explicitly.
1️⃣ Primitive Data Types
Data Type Example
String "Hello"
Number 100
Boolean true / false
Undefined var x;
Null null
var a = 40;
var b = "Hari";
2️⃣ Non-Primitive Data Types
Data Type Description
Object Stores properties
Array Stores multiple values
RegExp Pattern matching
var arr = [10, 20, 30];
5. JavaScript Variables
A variable is a storage location.
Types:
Local variable → Used inside a function
Global variable → Used anywhere in program
Rules for Variable Names
✔ Start with letter, _, or $
✔ Can contain numbers
✔ Case-sensitive
Correct Variables
var x = 10;
var _value = "Hari";
Incorrect Variables
var 123 = 30; // ❌
var *aa = 320; // ❌
Example Program
<script>
var x = 10;
var y = 20;
var z = x + y;
document.write(z);
</script>
Summary
JavaScript can be included in HTML using body section, inline code, or external files. It supports comments, variables, data types, and display functions like document.write, alert, and console.log. JavaScript is dynamic, easy to use, and widely used for creating interactive web pages.
How JavaScript Programs Are Written (C vs JS)
3️⃣ Factorial Program
<!DOCTYPE html>
<html>
<body>
<script>
let num = prompt("Enter a number:");
let fact = 1;
for (let i = 1; i <= num; i++) {
fact = fact * i;
}
document.write("Factorial = " + fact);
</script>
</body>
</html>
4️⃣ Prime Number Check
<!DOCTYPE html>
<html>
<body>
<script>
let num = prompt("Enter a number:");
let flag = true;
for (let i = 2; i < num; i++) {
if (num % i == 0) {
flag = false;
break;
}
}
if (flag && num > 1) {
document.write("Prime number");
} else {
document.write("Not a prime number");
}
</script>
</body>
</html>
5️⃣ Reverse a Number
<!DOCTYPE html>
<html>
<body>
<script>
let num = prompt("Enter a number:");
let rev = 0;
while (num > 0) {
rev = rev * 10 + (num % 10);
num = Math.floor(num / 10);
}
document.write("Reverse = " + rev);
</script>
</body>
</html>
6️⃣ Sum of Digits
<!DOCTYPE html>
<html>
<body>
<script>
let num = prompt("Enter a number:");
let sum = 0;
while (num > 0) {
sum = sum + (num % 10);
num = Math.floor(num / 10);
}
document.write("Sum of digits = " + sum);
</script>
</body>
</html>
7️⃣ Check Even or Odd
<!DOCTYPE html>
<html>
<body>
<script>
let num = prompt("Enter a number:");
if (num % 2 == 0) {
document.write("Even number");
} else {
document.write("Odd number");
}
</script>
</body>
</html>
8️⃣ Largest of Three Numbers
<!DOCTYPE html>
<html>
<body>
<script>
let a = parseInt(prompt("Enter first number"));
let b = parseInt(prompt("Enter second number"));
let c = parseInt(prompt("Enter third number"));
if (a > b && a > c) {
document.write("A is greatest");
} else if (b > c) {
document.write("B is greatest");
} else {
document.write("C is greatest");
}
</script>
</body>
</html>
To Compile and Run the JS code:
Method 1(Offline Editor):
Open Notepad/Vs Code/Sublime Text
Save file as palindrome.html
Open in browser
Enter input → See output
Method 2(Online Editor)
Use CodePen / JSFiddle
Or browser Console (F12)
NOTE: “JavaScript programs are written inside HTML and use prompt() for input and document.write() for output, just like scanf() and printf() in C.”


No comments:
Post a Comment