JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript Introduction


🔹 What is JavaScript?

JavaScript is a scripting language that allows you to create dynamic, interactive, and functional content on websites. It runs in the browser and enables you to handle:

  • Events

  • Validations

  • Dynamic content updates

  • Animations

  • API calls, and much more


🔹 Why Use JavaScript?

  • Adds interactivity to HTML pages

  • Responds to user actions (click, input, etc.)

  • Updates content without reloading the page

  • Validates user input before sending data to the server

  • Can communicate with servers using AJAX


🔹 JavaScript Syntax Example

<script>
  alert("Welcome to JavaScript!");
</script>

This pops up a message when the page loads.

 

🔹 Where Can JavaScript Be Placed?

1. Inside <script> tags in HTML
<script>
  document.getElementById("demo").innerHTML = "Hello!";
</script>
 
2. In the head or body of an HTML file
<head>
  <script>
    console.log("Page is loading...");
  </script>
</head>
 
3. External JavaScript file
<script src="script.js"></script>
 

And in script.js:

document.body.style.backgroundColor = "lightblue";
 

🔹 Output in JavaScript

Method Description
document.write() Writes directly to the HTML page
alert() Shows a popup box
console.log() Logs output to browser console
innerHTML Changes content of an element

🔹 JavaScript Variables

let x = 5;
const y = 10;
var name = "John";
 

Practice Questions

Q1. Show an alert message on page load.

Q2. Print a message in browser console.

Q3. Change the text of an HTML element.

Q4. Write content to the HTML page.

Q5. Change background color using JavaScript.

 

JavaScript Introduction Quiz

Q1: What is JavaScript primarily used for?

A. Structuring web pages
B. Styling web pages
C. Making web pages interactive
D. Creating databases

Q2: How do you write a comment in JavaScript?

A. <!-- comment -->
B. # comment
C. // comment
D. ** comment **

Q3: How do you declare a JavaScript variable?

A. variable x;
B. v x = 5;
C. let x = 5;
D. int x = 5;

Q4: What is the correct syntax to link an external JS file?

A. <script href="file.js">
B. <script src="file.js"></script>
C. <js link="file.js">
D. <script file="file.js">

Q5: Where is the correct place to insert JavaScript?

A. Only in the <head>
B. Only in the <body>
C. Either in <head> or <body>
D. Only in external files

Go Back Top