JavaScript is a lightweight, interpreted programming language primarily used to make web pages interactive. It allows developers to create features like sliders, pop-ups, form validation, and dynamic content updates without reloading the page.
JavaScript is:
strict mode, JSON, and more.let, const, arrow functions, classes, promises, etc.| Feature | JavaScript | Java | Python |
|---|---|---|---|
| Typing | Dynamic | Static | Dynamic |
| Execution | Interpreted (in browsers) | Compiled (JVM) | Interpreted |
| Use Case | Web interactivity, apps | Enterprise apps, Android | Data science, scripting |
| Syntax Simplicity | Medium | Complex | Simple |
| Popularity | Very High (Front-end dev) | High | High |
JavaScript is mainly used in the browser, unlike many languages that run on servers or local machines.
JavaScript can be added in three main ways:
You can write JavaScript directly inside an HTML element using the onclick, onmouseover, or similar attributes.
<button onclick="alert('Hello!')">Click Me</button>✅ Simple
❌ Not recommended for large projects (mixes JS with HTML)
JavaScript can be embedded within the <script> tag in the <head> or <body> of your HTML file.
<!DOCTYPE html>
<html>
<head>
<script>
function greet() {
alert("Welcome to JavaScript!");
}
</script>
</head>
<body>
<button onclick="greet()">Greet</button>
</body>
</html>✅ Good for small projects or testing
❌ Not reusable
JavaScript is placed in a separate .js file and linked using the <script src=""> tag.
index.html<script src="app.js"></script>app.jsfunction greet() {
alert("Hello from external JS!");
}✅ Clean, reusable, modular
✅ Recommended for real-world projects
JavaScript can output data in various ways:
Shows a pop-up message box to the user.
alert("Welcome!");Prints output to the browser's developer console. Useful for debugging.
console.log("This is a log message.");Writes directly into the HTML document.
document.write("Hello, World!");⚠️ Overwrites the whole document if used after the page loads — use with caution.
Changes the content of an HTML element.
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello from JS!";
</script>✅ Most commonly used for dynamic content updates