- What is JavaScript?
- Different Ways to Use JavaScript with HTML
- Famous Frontend Libraries
- "Hello World!" in JavaScript
- JS Comments
- JavaScript (JS) is a high-level, interpreted programming language that is primarily used to create interactive and dynamic content on websites.
- It was originally developed to add behavior and interactivity to static HTML pages.
- JavaScript is a core technology of the World Wide Web, alongside HTML and CSS.
- It can be used for a variety of tasks, including manipulating HTML and CSS, handling events, making network requests, and even building full-fledged web applications.
Key features of JavaScript include:
- Interactivity: Adds interactive elements like forms, buttons, and animations.
- Event Handling: Responds to user actions such as clicks, keystrokes, and form submissions.
- Cross-platform: Runs on most browsers, making it a versatile tool for web development.
- Client-Side Execution: Primarily executed in the browser, reducing server load.
There are several ways to integrate JavaScript with HTML:
JavaScript can be written directly within an HTML tag using the onclick, onmouseover, or similar attributes.
<button onclick="alert('Hello World!')">Click me</button>
JavaScript code can be placed within the <script>
tags in the HTML document's head or body section.
<html>
<head>
<script>
function sayHello() {
alert('Hello World!');
}
</script>
</head>
<body>
<button onclick="sayHello()">Click me</button>
</body>
</html>
JavaScript code can be written in a separate .js
file, which is then linked to the HTML document using the <script>
tag with the src
attribute.
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<button onclick="sayHello()">Click me</button>
</body>
</html>
script.js
function sayHello() {
alert('Hello World!');
}
JavaScript Modules:
JavaScript can be organized into modules for better code organization and reuse. Modules can be included in HTML using the type="module"
attribute in the <script>
tag.
<script type="module" src="module.js"></script>
- React.js
- Vue.js
- Next.js
- Angular
- Svelte
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hello World with Alert</title>
</head>
<body>
<script>
alert("Hello World!");
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hello World with Console</title>
</head>
<body>
<script>
console.log("Hello World!");
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hello World with Document Write</title>
</head>
<body>
<script>
document.write("Hello World!");
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hello World with DOM Manipulation</title>
</head>
<body>
<div id="output"></div>
<script>
document.getElementById("output").innerText = "Hello World!";
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hello World with External JS</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
script.js
alert('Hello World!');
// Change heading:
document.getElementById("myH").innerHTML = "My First Page";
// Change paragraph:
document.getElementById("myP").innerHTML = "My first paragraph.";
/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
in my web page:
*/
document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";