JavaScriptNotes_2
JavaScriptNotes_2
Introduction to JavaScript
What is JavaScript ?
JavaScript isa lightweight, cross-platform, single-threaded, and interpreted
compiled programming language. It is also known as the scripting language for webpages.
It is well-known for the development of web pages, and many non-browser environments
also use it.
JavaScript code is embedded within <script> and </script> tags when incorporated into an
HTML document. These scripts can be positioned within the body or head sections of an
HTML page, or even in both.
Alternatively, JavaScript can also be placed outside the HTML file and linked by specifying
its source in the script tag.
This basic HTML code includes JavaScript implementations using various approaches.
<!DOCTYPE html>
<html>
<head>
<title>
Add JavaScript Code inside Head
Section
</title>
</head>
<body>
<h2>
Adding JavaScript in HTML Document
</h2>
</html>
JavaScript code is placed inside the <head> section of an HTML page and uses
the <script> element. This ensures the script is loaded and executed when the page loads.
Example: This example shows the addition of a script file inside the head section.
<!DOCTYPE html>
<html>
<head>
<title>
Add JavaScript Code inside Head Section
</title>
<script>
function myFun() {
document.getElementById("demo").innerHTML = "Content
changed!";
}
</script>
</head>
<body>
<h2>
Add JavaScript Code
inside Head Section
</h2>
</html>
<!DOCTYPE html>
<html>
<head>
<title>
Add JavaScript Code inside Body Section
</title>
</head>
<body>
<h2>
Add JavaScript Code
inside Body Section
</h2>
<script>
function myFun() {
document.getElementById("demo")
.innerHTML = "Content changed!";
}
</script>
</body>
</html>
External JavaScript
JavaScript can also be used in external files. The file extension of the JavaScript file will
be .js. To use an external script put the name of the script file in the src attribute of
a <script> tag. External scripts cannot contain script tags.
Example: This example shows showing the linking of an external script file inside the head
section.
[File : ExtJavascript.html]
<!DOCTYPE html>
<html>
<head>
<title>
External JavaScript
</title>
<script src="script.js"></script>
</head>
<body>
<h2>
External JavaScript
</h2>
</html>
function myFun ()
{
document.getElementById('demo').innerHTML = 'Content Changed'
}
JavaScript Statements
let a, b, c;
a = 2;
b = 3;
c = a + b;
console.log("The value of c is " + c + ".");
Multiple statements on one line are allowed if they are separated with a
semicolon.
a = 2; b = 3; z = a + b;
Code Blocks
JavaScript statements can be grouped together inside curly brackets. Such
groups are known as code blocks. The purpose of grouping is to define
statements to be executed together.
Example: In this example, we have shown Code Blocks.
function myFunction()
{
console.log("Hello");
console.log("How are you?");
}
myFunction()
Keywords:
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Output</title>
</head>
<body>
<h2>
Display Output using innerHTML Property
</h2>
<p id="GFG"></p>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Output</title>
</head>
<body>
<h2>
Display Output using console.log() Method
</h2>
</html>
document.write();
Example :
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Output </title>
</head>
<body>
<h2>
Display Output using document.write() Method
</h2>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Output</title>
</head>
<body>
<h2>
Display Output using window.alert() Method
</h2>
</html>
<body>
<h2>JavaScript window.print() Method</h2>
<button onclick="window.print()">
Click here to Print
</button>
</body>
</html>
The window.prompt() method is used to display a dialog box that prompts the user input.
It returns the text entered by the user as a string. It doesn’t display output directly but
captures user input.
Note: This method is not used for output, it only use to take input from user.
Syntax:
window.prompt();
Example: The window.prompt() method to take user input and display the entered data
used alert() method.
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Output</title>
</head>
<body>
<h2>
Display prompt box for user input
</h2>
<script>
let userInput = window.prompt("Please Enter your Input");
</html>