Java Script by Shree
Java Script by Shree
Java Script by Shree
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>
Explanation - In the above example, texts in between /*
and */ (Using the Boolean method in the console.log()
shown below) are two multi-line comments in JavaScript.
Output:
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:
Commenting out code is important, especially when we
are debugging code. Because instead of removing code, we
can comment them out, so they won't be executed and
then uncomment whenever needed.
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.
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
Here, this page has finished loading. Now, if we click the
button, the document.write() method is called, which will
overwrite the existing page content with a new one.
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.
Initially, when we run the code, the web page is completely
loaded, which will print the text and show the button. At
this point, the onclick event is not triggered. Now, when
we click the button, the document.write() method is called,
which will overwrite everything on the webpage and print
the text inside it.
While the above four methods are the ones that are used
when we want to print some text or value to the terminal
or the console in JavaScript, there's one more method that
might give the impression that it is used to print a text to
the browser console or your terminal. This method is
called the window.print() method.
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:
let isCoding;
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>Variable Assignment</title>
</head>
<body>
<script>
let name = "Shrikant Kokate";
console.log(name);
const isCoding = "Yes";
console.log(isCoding);
var designation = "Senior Software Developer";
console.log(designation);
</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>Variable Assignment</title>
</head>
<body>
<script>
let age = 25;
console.log(age);
var year = 2022;
console.log(year);
const graduateYear = 2019;
console.log(graduateYear);
</script>
</body>
</html>
In the above code example, We assigned numeric values to
the variables instead of the text values that we had in the
previous example.
Output:
Types of Variables
Before we learn about the types of variables, let's first
understand about scope. A scope for reference can be
defined as the space in which a variable can be accessed
within a program in 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>Scope Example</title>
</head>
<body>
<h1>Global Scope and Local Scope.</h1>
<script>
let isCoding = true;
function normalFunction() {
let running = false;
console.log("Running 1:", running);
console.log("isCoding 1:", isCoding);
}
normalFunction();
console.log("isCoding 2:", isCoding);
console.log("isRunning 2:", running);
</script>
</body>
</html>
Output:
Only the let and const defined variables are blocked scope
variables, and the var keyword is either local scoped or
global scoped.
Now let's consider an example of block scope as well.
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>Block Scope Example</title>
</head>
<body>
<h1>Block scope.</h1>
<script>
function normalFunction() {
let running = false;
{
let age = 100;
console.log("Age 1: " + age);
}
console.log("Age 2: " + age);
}
normalFunction();
</script>
</body>
</html>
Output:
Variable Names in JavaScript
There are certain rules that we should follow when we are
declaring variables in JavaScript. These mainly are:
• Variables must start with a letter, an underscore(_), or
the dollar symbol ($).
• Variables cannot be named with numbers at the
starting index.
• Variables in JavaScript are case-sensitive.
• Keywords cannot be used as variable names.
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>Variable Convention Example</title>
</head>
<body>
<h1>Variable Convention.</h1>
<script>
// Valid Variable Declarations
let _name = “Shrikant”;
console.log('_name:' + _name);
let $age = 25;
console.log('Age: ' + $age);
let isStudent = false;
console.log('isStudent:' + isStudent);
// InValid Variable Declarations
let 1age = 25;
console.log('1age:' + 1age);
// Cannot use keywords as variable names
let var = 23;
console.log('var:' + var);
</script>
</body>
</html>
Output: