[go: up one dir, main page]

0% found this document useful (0 votes)
0 views5 pages

Upload 5

JavaScript is a versatile programming language used for creating dynamic web content, supporting both client-side and server-side applications. It can be embedded directly in HTML documents or linked as external files, with various methods for implementation including inline, internal, and external JavaScript. Learning JavaScript is beneficial for web development, game development, mobile apps, and even machine learning applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views5 pages

Upload 5

JavaScript is a versatile programming language used for creating dynamic web content, supporting both client-side and server-side applications. It can be embedded directly in HTML documents or linked as external files, with various methods for implementation including inline, internal, and external JavaScript. Learning JavaScript is beneficial for web development, game development, mobile apps, and even machine learning applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

https://www.geeksforgeeks.

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.

Webpage in Browser - JavaScript Tutorial

Why to learn JavaScript?

1. Versatility: JavaScript can be used to develop (using ElectronJS) websites, games


(Using Phaser and Three.js), mobile apps (using React Native), and more.

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.

<!--Driver Code Starts{-->

<html>

<head></head>

<body>

<h2>

Adding JavaScript in HTML Document

</h2>

<!--Driver Code Ends }-->

<button onclick="alert('Button Clicked!')">

Click Here

</button>

<!--Driver Code Starts{-->

</body>

</html>

<!--Driver Code Ends }-->

Internal JavaScript (Within <script> Tag)


You can write JavaScript code inside the <script> tag within the HTML file. This is known
as internal JavaScript and is commonly placed inside the <head> or <body> section of
the HTML document.

1. JavaScript Code Inside <head> Tag

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.

<!--Driver Code Starts{-->

<html>

2
<!--Driver Code Ends }-->

<head>

<script>

function myFun() {

document.getElementById("demo")

.innerHTML = "Content changed!";

</script>

</head>

<!--Driver Code Starts{-->

<body>

<h2>

Add JavaScript Code

inside Head Section

</h2>

<h3 id="demo" style="color:green;">

GeeksforGeeks

</h3>

<button type="button" onclick="myFun()">

Click Here

</button>

</body>

</html>

<!--Driver Code Ends }-->

2. JavaScript Code Inside <body> Tag

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.

<!--Driver Code Starts{-->

<html>

<head></head>

<!--Driver Code Ends }-->

3
<body>

<h2>

Add JavaScript Code

inside Body Section

</h2>

<h3 id="demo" style="color:green;">

GeeksforGeeks

</h3>

<button type="button" onclick="myFun()">

Click Here

</button>

<script>

function myFun() {

document.getElementById("demo")

.innerHTML = "Content changed!";

</script>

</body>

<!--Driver Code Starts{-->

</html>

<!--Driver Code Ends }-->

External JavaScript (Using External File)


For larger projects or when reusing scripts across multiple HTML files, you can
place your JavaScript code in an external .js file. This file is then linked to your
HTML document using the src attribute within a <script> tag.
HTML code
<!--Driver Code Starts{-->

<html>

<!--Driver Code Ends }-->

<head>

<script src="script.js"></script>

</head>

4
<!--Driver Code Starts{-->

<body>

<h2>

External JavaScript

</h2>

<h3 id="demo" style="color:green;">

GeeksforGeeks

</h3>

<button type="button" onclick="myFun()">

Click Here

</button>

</body>

</html>

<!--Driver Code Ends }-->

Javascript code
/* Filename: script.js*/

function myFun () {

document.getElementById('demo')

.innerHTML = 'Content Changed'

Advantages of External JavaScript

 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.

You might also like