JavaScript (JS) is a high-level, interpreted programming language primarily used for making web pages interactive and dynamic. It allows developers to control webpage behavior, update content dynamically, handle events, and interact with users.
JavaScript is a core technology of the web, along with:
✔ Interactivity – Enables animations, pop-ups, dropdowns, and user interactions.
✔ DOM Manipulation – Dynamically updates HTML & CSS (e.g., show/hide elements, change styles).
✔ Form Validation – Ensures correct user input before submission.
✔ Event Handling – Responds to user actions (clicks, key presses, etc.).
✔ Server Communication – Fetches and sends data via APIs (e.g., AJAX, Fetch API).
✔ Cross-Browser Compatibility – Runs on almost all web browsers.
✔ Client-Side Execution – Runs directly in the browser without requiring server interaction.
JavaScript can be added directly within an HTML element using the onclick
attribute:
<button onclick="alert('Hello!')">Click Me</button>
✔ Effect: Shows an alert box when the button is clicked.
JavaScript code can be placed inside a <script>
tag in an HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Example</title>
</head>
<body>
<script>
document.write("Welcome to JavaScript!");
</script>
</body>
</html>
✔ Effect: Displays "Welcome to JavaScript!" on the webpage.
JavaScript code can be written in a separate .js
file and linked using <script src="filename.js"></script>
:
HTML file (index.html
)
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript External Example</title>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
JavaScript file (script.js
)