1 JavaScriptIntroduction (ShreeNotes)
1 JavaScriptIntroduction (ShreeNotes)
Client-Side JavaScript
Client-side JavaScript is the most common form of the
language. The script should be included in or referenced by
an HTML document for the code to be interpreted by the
browser.
It means that a web page need not be a static HTML, but
can include programs that interact with the user, control
the browser, and dynamically create HTML content.
The JavaScript client-side mechanism provides many
advantages over traditional CGI server-side scripts. For
example, you might use JavaScript to check if the user has
entered a valid e-mail address in a form field.
The JavaScript code is executed when the user submits the
form, and only if all the entries are valid, they would be
submitted to the Web Server.
JavaScript can be used to trap user-initiated events such as
button clicks, link navigation, and other actions that the
user initiates explicitly or implicitly.
Beginner-Friendly Language
Unlike other programming languages, you can directly run
JavaScript code on web browsers. You can directly jump
into writing code without worrying about setting up the
environment first.
Not only that, you can build desktop, web, and mobile
applications by only using JS.
Most Popular Programming Language
Yes, it is the world's most popular programming language
and by a significant margin. There are many reasons that
contribute to its popularity.
Web Applications
As mentioned earlier, JavaScript frameworks like React,
Vue, Angular, and Node can be used to create full-fledged
web applications.
Web Development
From the early days, JavaScript was introduced to create
web pages. It provides simple ways to add dynamic
behaviors and special effects to the webpage.
Web Servers
With the introduction of Node.js, JavaScript developers
now can create web servers from the ground up and take
control of them.
Game Development
Yes, JavaScript can even create Games, and it's this
versatile nature of JavaScript that makes it so much
popular.
Mobile Applications
We can even use JavaScript to create mobile applications,
and one of the most popular frameworks to do the same is
React Native, which allows you to build cross-platform
mobile applications.
Art
Don't be surprised seeing this point here as JavaScript does
help in creating arts via Canvas elements. Creators can
even draw and create 2D and 3D graphics by using the
graphics on a web page.
For this we will use a HTML file and inside the HTML file,
we will add a script tag. We will then write our JavaScript
code inside that tag.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>Hello World - JavaScript Example</title>
</head>
<body>
<script>
console.log("Hello World");
</script>
</body>
</html>
Limitations of JavaScript
We cannot treat JavaScript as a full-fledged programming
language. It lacks the following important features:
Client-side JavaScript does not allow the reading or writing
of files. This has been kept for security reason.
JavaScript cannot be used for networking applications
because there is no such support available.
JavaScript doesn't have any multithreading or
multiprocessor capabilities. Once again, JavaScript is a
lightweight, interpreted programming language that allows
you to build interactivity into otherwise static HTML pages.
Syntax
JavaScript can be implemented using JavaScript statements
that are placed within the <script>... </script> HTML tags in
a web page.
You can place the <script> tags, containing your JavaScript,
anywhere within you web page, but it is normally
recommended that you should keep it within the <head>
tags.
The <script> tag alerts the browser program to start
interpreting all the text between these tags as a script. A
simple syntax of your JavaScript will appear as follows.
<script ...>
JavaScript code
</script>
The script tag takes two important attributes:
Language: This attribute specifies what scripting language
you are using. Typically, its value will be javascript. Although
recent versions of HTML (and XHTML, its successor) have
phased out the use of this attribute.
Case Sensitivity
JavaScript is a case-sensitive language. This means that the
language keywords, variables, function names, and any
other identifiers must always be typed with a consistent
capitalization of letters.
So the identifiers Time and TIME will convey different
meanings in JavaScript. NOTE: Care should be taken while
writing variable and function names in JavaScript.
2. Debugging
Comments are non-executable statements, so if we know
that a particular part of the code is causing an error, we
can easily comment on that part while debugging.
Types of Comments
In JavaScript, there are two types of comments. These are
1. Single Line Comment
2. Multi-Line Comment
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Comments in
JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Comments in JavaScript</h2>
<script>
let str = 0;
// Using the Boolean method
console.log(Boolean(str));
str = "";
// Using the Boolean method
console.log(Boolean(str));
</script>
</body>
</html>
Output:
Multi-Line Comment
The multi-line comment starts with the /* symbol and ends
with the */ symbol. Any text that is present between these
/* and */ symbols will be ignored by JavaScript.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Comments in
JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Comments in JavaScript</h2>
<script>
let str = 0;
/*
Using Boolean method
in the console.log()
shown below
*/
console.log(Boolean(str));
str = "";
/*
Using the Boolean method
in the console.log()
shown below
*/
console.log(Boolean(str));
</script>
</body>
</html>
Output:
Comments to prevent the execution of the code
We can even prevent some particular part of the code
from being executed if we comment on that part of the
code.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Comments in
JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Comments in JavaScript</h2>
<script>
let str = 0;
/*
commenting the console.log()
functions call below
*/
// console.log(Boolean(str));
str = "";
/*
Using Boolean method
in the console.log()
shown below
*/
console.log(Boolean(str));
</script>
</body>
</html>
Output:
Below are some of the ways with which you can distinguish
good and bad comments:
Bad Comments:
• Too much explanation
• Misleading data
• Not intention-revealing
• Redundant comments
Good Comments:
• Intent Explanation
• Informative
• Warning of Consequences
PLACEMENT OF SCRIPT TAG
There is a flexibility given to include JavaScript code
anywhere in an HTML document. However the most
preferred ways to include JavaScript in an HTML file are as
follows:
Script in <head>...</head> section.
Script in <body>...</body> section.
Script in <body>...</body> and <head>...</head> sections.
Script in an external file and then include in
<head>...</head> section.
In the following section, we will see how we can place
JavaScript in an HTML file
in different ways.
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--
document.write("Hello World")
//-->
</script>
<p>This is web page body </p>
</body>
</html>
console.log()
The console.log() method in JavaScript is used when we
want to write a message to the console. Whatever
message we want to print to the console, we pass it inside
the log() method as an argument.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>console.log() - JavaScript Example</title>
</head>
<body>
<p>
Some text.
</p>
<script>
console.log("Hello There!");
</script>
</body>
</html>
window.alert()
As the name suggests, the windows.alert() method prints
the output data as an alert in the browser. Let's see an
example.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>window.alert() - JavaScript Example</title>
</head>
<body>
<p>
Some text.
</p>
<script>
window.alert("Welcome to JavaScript")
</script>
</body>
</html>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>window.alert() - JavaScript Example</title>
</head>
<body>
<p>
Some text.
</p>
<script>
alert("Welcome to JavaScript ")
</script>
</body>
</html>
innerHTML()
innerHTML in JavaScript is used to add required texts to
the specific HTML element and print it.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>innerHTML - JavaScript Example</title>
</head>
<body>
<div id="simpleDiv">
This is a simple Div.
</div>
<script>
document.getElementById("simpleDiv").innerHTML =
"No, it is more than just a simple div"
</script>
</body>
</html>
document.getElementByID("simpleDiv").innerHTML = "No,
it is more than just a simple div"
We can even place a numeric value as well in the
innerHTML property.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>innerHTML - JavaScript Example</title>
</head>
<body>
<div id="simpleDiv">
This is a simple Div.
</div>
<script>
document.getElementById("simpleDiv").innerHTML = 13
</script>
</body>
</html>
Output:
document.write()
The document.write() method prints the output text on
the browser's webpage. Let's see an example.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>document.write() - JavaScript Example</title>
</head>
<body>
<script>
document.write("Shrikant.");
</script>
</body>
</html>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>document.write() - JavaScript Example</title>
</head>
<body>
<p>
Some text before the button.
</p>
<button onclick="document.write('New text
overwrites the existing text');" >
Click me!
</button>
</body>
</html>
Once we run the code, we will see the text inside the
paragraph tag and a button.
Output
Output
You can see that only "New text overwrites the existing
text" is shown now. Because of this behavior, we should
never use document.write() after the page has finished
loading.
Working of document.write()
In the above code, the p tag is printing some text and the
button has an onclick event set up. Here, when we click the
button, the document.write() method is called.
window.print()
The window.print() method is different from all the
methods that are mentioned above as it is used when we
want the browser to print the content that we have on the
current browser window.
JavaScript ,by default, doesn't have any print method.
Consider the index.html code shown below.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>window.print() - JavaScript Example</title>
</head>
<body>
<p>
Some text.
</p>
<button onclick="window.print();">
Click to Print!
</button>
</body>
</html>
Output:
After clicking on the button, you will be prompted with a
printing screen that will ask you whether you want to print
the content of the current window or not.