Jscript 1 1
Jscript 1 1
page
1) <html> 2) <html>
<head> <head>
<title>Hello JavaScript</title> <title>Hello JavaScript</title>
</head>
<script type="text/javascript“ >
document.write("Hello World!"); <body>
</script> <script type="text/javascript“ >
document.write("Hello World!");
</head> </script>
</body>
<body>
</body> </html>
</html>
JAVA SCRIPT IN HTML
• Write java script in a separate file with .js
extension.
• Include the external javascript file (.js) in HTML
as follows
<html>
<head>
<script type=“text/javascript” src=“hello.js”>
</script>
</head>
<body>
</body>
</html>
JAVA SCRIPT-Variables
Used to store information
Variable name are case sensitive
•Names can contain letters, digits, underscores, and
dollar signs.
•Names must begin with a letter
•Names can also begin with $ and _
•Names are case sensitive (y and Y are different
variables)
•Reserved words (like JavaScript keywords) cannot
be used as names
•Can be declared with var or let or const
JAVA SCRIPT-Variables
// declaring single variable
var name;
// initializng variables
var name = "Harsh";
name = "Rakesh";
JAVA SCRIPT-Variables
// creating variable to store a number
var num = 5;
// store string in the variable num
num = "GeeksforGeeks";
// storing a mathematical expression
var x = 5 + 10 + 1;
console.log(x); // 16
JAVA SCRIPT-Variables
// let variable
let x; // undefined
var x = 1;
if (x == 1)
{
let x = 2;
document.write(x); // expected output: 2
}
document.write(x);// expected output: 1
JAVA SCRIPT-Variables
The var statement declares a function-scoped or
globally-scoped variable, optionally initializing it
to a value.
var x = 1;
if (x == 1)
{
var x = 9;
document.write(x); // expected output: 9
}
document.write(x);// expected output: 9
JAVA SCRIPT-Variables Scope
• The scope of a variable is the region of your program in
which it is defined.
• JavaScript variables have only two scopes.
<head>
<script type="text/javascript">
</script>
</head>
Functions in Javascript
• isFinite() — Determines whether a passed value is a
finite number
• isNaN() — Determines whether a value is Not a
Number or not .
• parseFloat() — Parses an argument and returns a
floating point number
• parseInt() — Parses its argument and returns an
integer
Objects in JavaScript
• Array object
• String object
• Math object
• Date object
Array Object
An array is a special variable, which can hold more than one
value, at a time.
An array can be defined in three ways.
The following code creates an Array object called myCars:
1: var myCars=new Array(); // regular array
myCars[0]="Saab";
myCars[1]="Volvo";
myCars[2]="BMW";
Syntax: array.join(separator)
Example:
var fruits = ["Orange", "Apple", "Mango"];
document.write(fruits.join() + "<br />");
document.write(fruits.join("+") + "<br />");
document.write(fruits.join(" and "));
Array-predefined methods
concat():
The concat() method is used to concatenate the two
arrays and it gives the merged array.
document.write(num1.concat(num2, num3));
Array-predefined methods
push(): Adding Element at the end of an Array
Array.push(item1, item2 …)
var number_arr = [10, 20, 30, 40, 50];
number_arr.push(60);
number_arr.splice(1, 3);
number_arr.splice(1, 0, 3, 4, 5);
Array-predefined methods
reverse() : The reverse() method reverses the elements in an
array.
•var x = "John";
var y = new String("John");
String Object
• Length property
• length property returns the length of a string:
<script type="text/JavaScript">
var txt = "Hello World!";
document.write(txt.length);
</script>;
String Object-Methods
String Object-Methods
• charCodeAt():Returns the Unicode of the
character at the specified index
• Return the Unicode of the first character in a
string:
<script type="text/javascript">
var str = "Hello world!";
document.write("First character: " +
str.charCodeAt(0) + "<br />");
</script>
String Object-Methods
String Object-Methods
String Object-Methods
String Object-Methods
String Object-Methods
String Object-Methods
Math Object
Math Object
• abs(x) :Returns the absolute value of x
<script type="text/javascript">
document.write(Math.abs(7.25) + "<br />"); // 7.25
document.write(Math.abs(-7.25) + "<br />"); // 7.25
document.write(Math.abs(null) + "<br />"); // 0
document.write(Math.abs("Hello") + "<br />"); // NaN
document.write(Math.abs(2+3)); //5
</script>
Math Object
• ceil(x):Returns x, rounded upwards to the
nearest integer