Upload 5
Upload 5
org/javascript/
JavaScript
JavaScript is a programming language used to create dynamic content for websites. It is
a lightweight, cross-platform, and single-threaded programming language. JavaScript is
an interpreted language that executes code line by line providing more flexibility.
HTML adds Structure to a web page, CSS styles it and JavaScript brings it to life by allowing
users to interact with elements on the page, such as actions on clicking buttons, filling out
forms, and showing animations.
JavaScript on the client side is directly executed in the user's browser. Almost all browsers
have JavaScript Interpreter and do not need to install any software. There is also a browser
console where you can test your JavaScript code.
JavaScript is also used on the Server side (on Web Servers) to access databases, file handling
and security features to send responses, to browsers.
2. Client Side: JavaScript is the main language for client-side logic and is supported by almost all
browsers. There is a big list of frameworks and libraries like React JS, Angular JS, and Vue JS.
3. Server-Side: With runtime environments like Node.js and Frameworks like Express.js,
JavaScript is now widely used for building server-side applications.
4. Machine Learning: With Libraries like Tensorflow.JS, JavaScript can be used to develop and
train machine learning models. Please refer to ML in JS for details.
1
How to Add JavaScript in HTML
Document?
https://www.geeksforgeeks.org/where-to-put-javascript-in-an-html-document/
To add JavaScript in HTML document, several methods can be used. These
methods include embedding JavaScript directly within the HTML file or linking an
external JavaScript file.
Inline JavaScript
You can write JavaScript code directly inside the HTML element using
the onclick, onmouseover, or other event handler attributes.
<html>
<head></head>
<body>
<h2>
</h2>
Click Here
</button>
</body>
</html>
Placing JavaScript within the <head> section of an HTML document ensures that the
script is loaded and executed as the page loads. This is useful for scripts that need to be
initialized before the page content is rendered.
<html>
2
<!--Driver Code Ends }-->
<head>
<script>
function myFun() {
document.getElementById("demo")
</script>
</head>
<body>
<h2>
</h2>
GeeksforGeeks
</h3>
Click Here
</button>
</body>
</html>
JavaScript can also be placed inside the <body> section of an HTML page. Typically,
scripts placed at the end of the <body> load after the content, which can be useful if
your script depends on the DOM being fully loaded.
<html>
<head></head>
3
<body>
<h2>
</h2>
GeeksforGeeks
</h3>
Click Here
</button>
<script>
function myFun() {
document.getElementById("demo")
</script>
</body>
</html>
<html>
<head>
<script src="script.js"></script>
</head>
4
<!--Driver Code Starts{-->
<body>
<h2>
External JavaScript
</h2>
GeeksforGeeks
</h3>
Click Here
</button>
</body>
</html>
Javascript code
/* Filename: script.js*/
function myFun () {
document.getElementById('demo')
Faster Page Load Times: Cached external JavaScript files don’t need to be reloaded every
time the page is visited, which can speed up loading times.
Improved Readability and Maintenance: Keeping HTML and JavaScript separate makes both
easier to read and maintain.
Separation of Concerns: By separating HTML (structure) and JavaScript (behavior), your code
becomes cleaner and more modular.
Code Reusability: One external JavaScript file can be linked to multiple HTML files, reducing
redundancy and making updates easier.