C6 JavaScript
C6 JavaScript
• ORIGIN OF JAVASCRIPT
– JavaScript originated with Netscape and it is
invented by Brendan eich, it was initially named
mocha.
– Then it is renamed as liveScript.
– In late 1995 LiveScript became a joint venture of
Netscape and Sun Microsystems, and its name
again was changed, this time to JavaScript.
– A language standard for JavaScript was developed
in the late 1990s by the European Computer
Manufacturers Association (ECMA) as ECMA-262.
– The official name of the standard language is
ECMAScript.
• Components of JavaScript
– JavaScript can be divided into three parts: the
core, client side, and server side.
– The core is the heart of the language, including its
operators, expressions, statements, and
subprograms.
– Client-side JavaScript is a collection of objects that
support the control of a browser and interactions
with users.
– Server-side JavaScript is a collection of objects
that make the language useful on a Web server.
• Difference between object based and object oriented.
– Object-oriented languages do not have the inbuilt objects
• Displaying popup windows and dialog boxes (like alert dialog box,
confirm dialog box and prompt dialog box)
<head>
<script type="text/javascript" src="message.js"></script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
• JavaScript Comment:
– Standard java- style comment lines and blocks are
supported in JavaScript also.
– In general, any text inside a comment block is
ignored by the interpreter.
– Single-line comments are preceded by a double
forward-slash(//), and multiline comments are
enclosed by the symbols /* and */.
• JavaScript keywords:
– Keywords are the reserved and cannot be used as
identifiers. The complete list is:
• Primitive Data Types
– Every variable has a data type. Data type indicates
what kind of data the variable holds. In JavaScript,
the type of data variable hold can be grouped into
two categories:
• Primitive types
• Composite types
o/p: Line 1
Line 2
• JavaScript Operators
– JavaScript operators are symbols that are used to perform
operations on operands. For example:
var sum=10+20;
– Here, + is the arithmetic operator and = is the assignment
operator.
<script>
var a=20;
if(a%2==0){
document.write("a is even number");
Output of the above example
}
a is even number
else{
document.write("a is odd number");
}
</script>
• JavaScript If...else if statement
– It evaluates the content only if expression is true from several
expressions. The signature of JavaScript if else if statement is given
below.
if(expression1){ <script>
//content to be evaluated if expression1 is true var a=20;
} if(a==10){
document.write("a is equal to 10");
else if(expression2){ }
//content to be evaluated if expression2 is true else if(a==15){
} document.write("a is equal to 15");
}
else if(expression3){ else if(a==20){
//content to be evaluated if expression3 is true document.write("a is equal to 20");
} }
else{ else{
document.write("a is not equal to 10, 15 or 20");
//content to be evaluated if no expression is true }
} </script>
• while loop
• do-while loop
• JavaScript For loop
– The JavaScript for loop iterates the elements for the fixed
number of times. It should be used if number of iteration
is known. The syntax of for loop is given below.
for (initialization; condition; increment) <script>
{ for (i=1; i<=5; i++)
code to be executed {
} document.write(i + "<br/>")
}
Output: </script>
1
2
3
4
5
• JavaScript while loop
– The JavaScript while loop iterates the elements for the
infinite number of times. It should be used if number of
iteration is not known. The syntax of while loop is given
below.
<script>
while (condition) var i=11;
{ while (i<=15)
code to be executed {
} document.write(i + "<br/>");
i++;
}
Output: </script>
11
12
13
14
15
• JavaScript do while loop
– The JavaScript do while loop iterates the elements for the infinite
number of times like while loop. But, code is executed at least once
whether condition is true or false. The syntax of do while loop is given
below.
<script>
do{ var i=21;
code to be executed do{
}while (condition); document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
Output:
21
22
23
24
25
• JavaScript Functions
– JavaScript functions are used to perform operations. We
can call JavaScript function many times to reuse the code.
//code to be executed
}
<script>
function msg(){
alert("hello! this is message");
}
</script>
<input type="button" onclick="msg()" value="call function"/>
<script>
function getcube(number){
alert(number*number*number);
}
</script>
<form>
<input type="button" value="click" onclick="getcube(4)"/>
</form>
• Function with Return Value
– We can call function that returns a value and use it in our program.
Let’s see the example of function that returns value.
<script>
function getInfo(){
return "hello javatpoint! How r u?";
}
</script>
<script>
document.write(getInfo());
</script>
• JavaScript Objects
– A JavaScript object is an entity having state and
behavior (properties and method). For example: car,
pen, bike, chair, glass, keyboard, monitor etc.
– JavaScript is an object-based language. Everything is
an object in JavaScript.
<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
concat() It returns a new array object that contains two or more merged arrays.
indexOf() It searches the specified element in the given array and returns the index
of the first match.
slice() It returns a new array containing the copy of the part of the given array.
unshift() It adds one or more elements in the beginning of the given array.
Concat()
<script>
var arr1=["C","C++","Python"];
var arr2=["Java","JavaScript","Android"];
var result=arr1.concat(arr2);
document.writeln(result);
</script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let size = fruits.length;
<script>
var stringname=new String("hello javascript string");
document.write(stringname);
</script>
• JavaScript String Methods
Methods Description
charAt() It provides the char value present at the specified index.
concat() It provides a combination of two or more strings.
indexOf() It provides the position of a char value present in the given string.
lastIndexOf() It provides the position of a char value present in the given string by
searching a character from the last position.
search() It searches a specified regular expression in a given string and returns its
position if a match occurs.
replace() It replaces a given string with the specified replacement.
substr() It is used to fetch the part of the given string on the basis of the specified
starting position and length.
slice() It is used to fetch the part of the given string. It allows us to assign
positive as well negative index.
toLowerCase() It converts the given string into lowercase letter.
toUpperCase() It converts the given string into uppercase letter.
split() It splits a string into substring array, then returns that newly created array.
trim() It trims the white space from the left and right side of the string.
• JavaScript String charAt(index) Method
– The JavaScript String charAt() method returns the
character at the given index.
<script>
var str="javascript";
document.write(str.charAt(2));
</script>
• Constructor
– You can use 4 variant of Date constructor to create date object.
• Date()
• Date(milliseconds)
• Date(dateString)
• Date(year, month, day, hours, minutes, seconds, milliseconds)
• JavaScript Date Methods
Methods Description
getDate() It returns the integer value between 1 and 31
that represents the day for the specified date
on the basis of local time.
getHours() It returns the integer value between 0 and 23
that represents the hours on the basis of
local time.
getMilliseconds() It returns the integer value between 0 and
999 that represents the milliseconds on the
basis of local time.
getMinutes() It returns the integer value between 0 and 59
that represents the minutes on the basis of
local time.
<script>
var date=new Date();
var day=date.getDate();
var month=date.getMonth()+1;
var year=date.getFullYear();
document.write("<br>Date is: "+day+"/"+month+"/"+year);
</script>
<script>
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
</script>
• JavaScript Math
– The JavaScript math object provides several
constants and methods to perform mathematical
operation.
• JavaScript Math Methods
Methods Description
Modifier Description
i Perform case-insensitive matching
g Perform a global match (find all matches rather than stopping after the first
match)
m Perform multiline matching
Expression Description
[abc] Find any of the characters between the brackets
[0-9] Find any of the digits between the brackets
(x|y) Find any of the alternatives separated with |
• Metacharacters are characters with a special meaning:
Metacharacter Description
\d Find a digit
\s Find a whitespace character
\b Find a match at the beginning of a word like this: \
bWORD, or at the end of a word like this: WORD\b
\uxxxx Find the Unicode character specified by the hexadecimal
number xxxx
• Using the RegExp Object
– In JavaScript, the RegExp object is a regular expression object with
predefined properties and methods.
– Using test()
• The test() method is a RegExp expression method.
• It searches a string for a pattern, and returns true or false, depending on the result.
• The following example searches a string for the character "e":
true
– Using exec()
• The exec() method is a RegExp expression method.
• It searches a string for a specified pattern, and returns the found text as an object.
• If no match is found, it returns an empty (null) object.
• The following example searches a string for the character "e":