Practical-1 1
Practical-1 1
[2ES111]
Computer Engineering & Information
Technology
Semester- 2
2. JavaScript Basic:
JS Introduction, JS Output, JS Statements, JS Syntax, JS Comments, JS Variables, JS
Operators, JS Data Types
JS Introduction:
JavaScript is a very powerful client-side scripting language. JavaScript is used mainly for
enhancing the interaction of a user with the webpage. JavaScript was initially created to “make
web pages alive”. The programs in this language are called scripts. you can make your webpage
more lively and interactive, with the help of JavaScript. JavaScript is also being used widely in
game development and Mobile application development. JavaScript and Java are very much
unrelated. Java is a very complex programming language whereas JavaScript is only a
scripting language. The syntax of JavaScript is mostly influenced by the programming language
C.
JavaScript can execute not only in the browser, but also on the server, or actually on any device
that has a special program called the JavaScript engine. The browser has an embedded engine
sometimes called a “JavaScript virtual machine”.
Different engines have different “codenames”. For example:
V8 – in Chrome and Opera.
SpiderMonkey – in Firefox.
Add new HTML to the page, change the existing content, modify styles.
React to user actions, run on mouse clicks, pointer movements, key presses.
Send requests over the network to remote servers, download and upload files (so-
called AJAX and COMET technologies).
Get and set cookies, ask questions to the visitor, show messages.
Remember the data on the client-side (“local storage”).
To start with JS, you need a text editor to write your code and a browser to display the web
pages you develop. You can use a text editor of your choice including Notepad++, Visual
Studio Code, Sublime Text, Atom or any other text editor you are comfortable with. You
can use any web browser including Google Chrome, Firefox, Microsoft Edge, Internet
Explorer etc.
The browser is responsible for running JavaScript code. When a user requests an HTML
page with JavaScript in it, the script is sent to the browser and it is up to the browser to
execute it. The main advantage of JavaScript is that all modern web browsers support
JavaScript. So, you do not have to worry about whether your site visitor uses Internet
Explorer, Google Chrome, Firefox or any other browser. JavaScript will be supported. Also,
JavaScript runs on any operating system including Windows, Linux or Mac.
You should place all your JavaScript code within <script> tags (<script> and </script>) if
you are keeping your JavaScript code within the HTML document itself. or you can also
make script file with .js extension.
Code:
<html>
<head>
<title>My First JavaScript code!!!</title>
<script type="text/javascript">
alert("Hello World!");
</script>
</head>
<body>
</body>
</html>
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.
JS Output:
There are four different ways to display the output in JavaScript.
Code:
<html>
<body>
<p id="display"></p>
<script>
document.getElementById("display").innerHTML = 10 + 10;
</script>
</body>
</html>
Output:
20.
<script>
document.write(5 + 3);
</script>
Output:
<script>
console.log(2+ 3);
</script>
<script>
alert(2+ 3);
</script>
Output:
JS Statements:
Example:
This statement tells the browser to write "Hello Dolly." inside an HTML element with
id="demo".
The statements are executed, one by one, in the same order as they are written. where each
statements are separated by semicolon ";"
JS Keywords:
JavaScript statements often start with a keyword to identify the JavaScript action to be
performed.
JS 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 your 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.
syntax:
<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.
Type − This attribute is what is now recommended to indicate the scripting language in
use and its value should be set to "text/javascript".
JS Comments:
The JavaScript comments are meaningful way to deliver message. It is used to add information
about the code, warnings or suggestions so that end user can easily interpret the code.
The JavaScript comment is ignored by the JavaScript engine i.e. embedded in the browser.
1. To make code easy to understand It can be used to elaborate the code so that end user
can easily understand the code.
2. To avoid the unnecessary code It can also be used to avoid the code being executed.
Sometimes, we add the code to perform some action. But after sometime, there may be
need to disable the code. In such case, it is better to use comments.
1. Single-line Comment:
It is represented by double forward slashes (//). It can be used before and after the
statement.
Example::
<script>
// It is single line comment
document.write("hello javascript");
</script>
2. Multi-line Comment:
It can be used to add single as well as multi line comments. So, it is more convenient. It is
represented by forward slash with asterisk then asterisk with forward slash. It can be
used before, after and middle of the statement.
Example:
Code:
<script>
/* It is multi line comment.
It will not be displayed */
document.write("example of javascript multiline comment");
</script>
JS Variables:
A JavaScript variable is simply a name of storage location. There are two types of variables in
JavaScript : local variable and global variable.
There are some rules while declaring a JavaScript variable (also known as identifiers).
Code:
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
Output:
30
<script>
function abc(){
var x=10;//local variable
}
</script>
JS global variable: It is accessible from any function. A variable i.e. declared outside the
function or declared with window object is known as global variable For example:
<script>
var value=50;//global variable
function a(){
alert(value);
}
function b(){
alert(value);
}
</script>
To declare JavaScript global variables inside function, you need to use window object. For
example:
window.value=90;
Now it can be declared inside any function and can be accessed from any function. For example:
function m(){
window.value=100;//declaring global variable by window object
}
function n(){
alert(window.value);//accessing global variable from other function
}
When you declare a variable outside the function, it is added in the window object internally.
You can access it through window object also. For example:
var value=50;
function a(){
alert(window.value);//accessing global variable
}
JS Operators:
JavaScript operators are symbols that are used to perform operations on operands. For example:
var sum=10+20;
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Special Operators
JS Datatypes:
JavaScript provides different data types to hold different types of values. There are two types of
data types in JavaScript.
JavaScript is a dynamic type language, means you don't need to specify type of the variable
because it is dynamically used by JavaScript engine. You need to use var here to specify the data
type. It can hold any type of values such as numbers, strings etc. For example:
Type Conversions:
Most of the time, operators and functions automatically convert a value to the right type. That’s
called “type conversion”.
For example, alert automatically converts any value to a string to show it. Mathematical
operations convert values to numbers.
There are also cases when we need to explicitly convert a value to put things right.
ToString
ToNumber
Explicit conversion is usually required when we read a value from a string-based source like a
text form, but we expect a number to be entered.
If the string is not a valid number, the result of such conversion is NaN, for instance:
let age = Number("an arbitrary string instead of a number");
alert(age); // NaN, conversion failed
Value Becomes...
Undefined NaN
Null 0
True and false 1 and 0
ToBoolean
Exercise:
var
one
1,
two
"two"
document.getElementById("p1").innerHTML = one;
document.getElementById("p2").innerHTML = two;
</script>
<script type="text/javascript">
var two = 3;
document.write("First No: = " + one + "<br />Second No: = " + two + " <br
/>");
</script>
and
<script>
var x = "Volvo"+16 + 4 ;
document.getElementById("demo").innerHTML = x;
</script>
<script>
myVar = true;
myVar = null;
myVar = undefined;
myVar = "Steve";
alert(myVar);
</script>
"" + 1 + 0
"" - 1 + 0
true + false
6 / "3"
"2" * "3"
4 + 5 + "px"
"$" + 4 + 5
"4" - 2
"4px" - 2
7 / 0
" -9\n" + 5
" -9\n" - 5
null + 1
undefined + 1