IT3401-WE Unit 3
IT3401-WE Unit 3
Introduc on to JavaScript
Part-b
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 c an hold any
type of values such as numbers, strings etc. For example:
There are five types of primi ve data types in JavaScript. They are as follows:
1. var a=40;//holding number
A JavaScript variable is simply a name of storage loca on. There are two types of variables in JavaScript :
There are some rules while declaring a JavaScript variable (also known as iden fiers).
3. JavaScript variables are case sensi ve, for example x and X are different variables.
global variable.
1. var x = 10;
2. var _value="sonoo";
A JavaScript local variable is declared inside block or func on. It is accessible within the func on or block
Example of JavaScript variable
<script> var
x = 10; var
y = 20; var
z=x+y;
document.w
rite(z);
</script>
30
x=10;//local variable
</script>
A JavaScript global variable is accessible from any func on. A variable i.e. declared outside the func on
or declared with window object is known as global variable. For example:
<script> var
data=200;//gloabal variable
func on a(){
document.writeln(data);
func on b(){
document.writeln(data);
b();
</script>
JavaScript Operators
JavaScript operators are symbols that are used to perform opera ons on operands. For example:
+ Addi on 10+20 = 30
- Subtrac on 20-10 = 10
var sum=10+20;
/ Division 20/10 = 2
1. Arithme c Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Special Operators
Arithme c operators are used to perform arithme c opera ons on the operands. The following operators
are known as JavaScript arithme c operators.
follows:
JavaScript Bitwise Operators
The bitwise operators perform bitwise opera ons on operands. The bitwise operators are as follows:
Operator Descrip on
(?:) Condi onal Operator returns value based on the condi on. It is like if-else.
= Assign 10+10 = 20
Operator precedence:
Mul plica on (*) and division (/) have higher precedence than addi on (+) and subtrac on (-).
Operator precedence describes the order in which opera ons are performed in an arithme c expression.
Math Property Descrip on
SQRT2 Returns
square root
of 2.
PI Returns Π
value.
E\ Returns
Euler's
Constant.
LN2 Returns
natural
logarithm of
2.
3. Brief about various date and math related object with examples?
Math Object
Math Proper es
LN10 Returns natural logarithm of 10.
Math Methods
Methods Descrip on
<html>
<head>
< tle>JavaScript Math Object Methods</ tle>
</head>
<body>
<script type="text/javascript">
Output
2. Date Object
Date() constructor takes no arguments. • Date object allows you to get and
set the year, month, day, hour, minute, second and millisecond fields.
Syntax:
var variable_name = new Date();
Example:
var current_date = new Date();
Date Methods
Methods Descrip on
getTime() Returns the number of milliseconds since January 1, 1970 at 12:00 AM.
getTimezoneOffset() Returns the mezone offset in minutes for the current locale.
<html>
<body>
<center>
<h2>Date Methods</h2>
<script type="text/javascript">
var d = new Date();
</center>
</body>
</html> Output:
4. Write a JavaScript code for valida ng new user registra on form which includes the fields like
username, password, confirm password, gender, date of birth, contact number and mail id.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{ font-family:
Calibri;
input[type="text"] {
width: 250px;
input[type="submit"], input[type="reset"] {
180px;
}
form{ text-align:
center; font-family:
20px auto;
td {
padding: 10px;
td:first-child {
weight: bold;
td:last-child {
text-align: le ;
</style> <script>
func on validate() {
alert("Name is required");
fname.focus(); return
false;
if (lname.value.length <= 0) {
if (address.value.length <= 0) {
alert("Address is required");
address.focus(); return
false;
if (gender.value.length <= 0) {
alert("Gender is required");
gender.focus(); return
false;
if (email.value.length <= 0) {
alert("Email Id is required");
if (mobile.value.length <= 0) {
return false;
return false;
</script>
</head>
<body>
<hr>
<table>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<td><label>Mobile: </label></td>
<input type="text" name="lname" placeholder="Last Name">
</td>
</tr>
<tr>
<td><label>Address: </label></td>
<td>
</td>
</tr>
<tr>
<td><label>Gender: </label></td>
<td>
</td>
</tr>
<tr>
<td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td><label>Course: </label></td>
<td>
<select name="course">
</select>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>
Output