CSS Chapter 2
CSS Chapter 2
Unit - II
• Array elements may even be objects or other arrays, which allows you
to create complex data structures, such as arrays of objects and arrays
of arrays. Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
2
MHSSP
Array in Java Script
• JavaScript arrays are dynamic: they grow or shrink as needed and there
is no need to declare a fixed size for the array when you create it or to
reallocate it when the size changes.
• Array literal syntax allows an optional trailing comma, so [,,] has only
two elements, not three.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
5
MHSSP
Creating Array in Java Script
• Another way to create an array is with the Array() constructor. You can invoke this
constructor in three distinct ways:
• Call it with no arguments:
var a = new Array();
• This method creates an empty array with no elements and is equivalent to the
array literal [].
• In this form, the constructor arguments become the elements of the new array.
• Using an array literal is almost always simpler than this usage of the Array()
constructor.
• You can use this syntax to both read and write the value of an element of an
array. Thus, the following are all legal JavaScript statements:
var a = ["world"]; // Start with a one-element array
var value = a[0]; // Read element 0
a[1] = 3.14; // Write element 1
i = 2;
a[i] = 3; // Write element 2
a[i + 1] = "hello"; // Write element 3
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
8
MHSSP
Creating Array in Java Script
<html>
<head>
<title>
Create an Array
</title>
</head>
<body>
<script>
var cars=["audi","volvo","bmw"];
document.write(cars+"<br>");
document.write(cars[0]+"<br>");
document.write(cars[1]+"<br>");
document.write(cars[2]+"<br>");
document.write("length of array is : "+cars.length);
</script>
</body>
</html> Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
MHSSP
9
Sparse Array in Java Script
• A sparse array is one in which the elements do not have contiguous indexes
starting at 0.
• Normally, the length property of an array specifies the number of elements in the
array.
• If the array is sparse, the value of the length property is greater than the number
of elements.
• Sparse arrays can be created with the Array() constructor or simply by assigning
to an array index larger than the current array length.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
10
MHSSP
Sparse Array in Java Script
E.g. a = new Array(5); // No elements, but a.length is 5.
a = []; // Create an array with no elements and length = 0.
a[1000] = 0; // Assignment adds one element but sets length to 1001.
• Note that when you omit value in an array literal, you are not creating a sparse
array. The omitted element exists in the array and has the value undefined.
• For arrays that are dense (i.e., not sparse), the length property specifies the
number of elements in the array. Its value is one more than the highest index in
the array:
E.g.
[].length // => 0: the array has no elements
['a','b','c'].length // => 3: highest index is 2, length is 3
• Doing this does not actually add any new elements to the array, it simply creates
a sparse area at the end of the array.
e.g.
a = [1,2,3]; // Start with a 3-element array.
Object.defineProperty(a, "length", // Make the length property
{writable: false}); // readonly.
a.length = 0; // a is unchanged.
• Unlike delete, the shift() method shifts all elements down to an index one lower
than their current index.
• Global Object:
• When a function is called without an owner object, the value of this becomes
the global object.
• In a web browser the global object is the browser window.
• If a value is primitive (int, string, Boolean etc) or another object , the value is
considered as property.
• An identifier that names the function. The name is a required part of function
declaration statements: For function definition expressions, the name is
optional: if present, the name refers to the function object only within the
body of the function itself.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
34
MHSSP
Defining Functions
• A pair of parentheses around a comma-separated list of zero or more
identifiers. These identifiers are the parameter names for the function, and
they behave like local variables within the body of the function.
• A pair of curly braces with zero or more JavaScript statements inside. These
statements are the body of the function: they are executed whenever the
function is invoked.
function myfunction(a,b)
{
return(a*b);
}
</script>
</body>
</html> Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
36
MHSSP
Invoking Functions
• The JavaScript code that makes up the body of a function is not
executed when the function is defined but when it is invoked.
• JavaScript functions can be invoked in four ways:
• as functions,
• as methods,
• as constructors
• Since a function is not associated with any user-defined object but with
a global object (window).
• Syntax:
function_name(parameter list);
• Syntax:
object.method_name(parameter_list);
• It looks like you create a new function, but since JavaScript functions
are objects you actually create a new object:
• Syntax:
var name = new function_name(parameter_list);
• Syntax:
string1.concat(string2,string3,…)
• E.g.:
var text1 = "Hello";
var text2 = "World";
var text3 = text1.concat(" ", text2);
document.write(text3);
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
48
MHSSP
charAt(x)
• Syntax:
variable = charAt(position)
• E.g.
var myString = ‘Hello World!!!';
document.write(myString.charAt(7));
//output: W
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
49
MHSSP
indexOf()
• This method returns the index of (position of) the first occurrence of a
specified text in a string or a character.
• Returns -1 if the text is not found.
• Syntax:
indexOf(string/character, start_position);
• Where: string or character is the item to be searched
• Start_position: starting position to start the search with
• separator is optional. Specifies the character, or the regular expression, to use for splitting
the string. If omitted, the entire string will be returned (an array with only one item)
Limit: Optional. An integer that specifies the number of splits, items after the split limit will
not be included in the array
• If "start" is greater than "end", this method will swap the two
arguments, meaning str.substring(1, 4) == str.substring(4, 1).
• Where:
start: Required. The position where to start the extraction. First character is at index 0
• End: Optional. The position (up to, but not including) where to end the extraction. If
omitted, it extracts the rest of the string
• The method takes 2 parameters: the start position, and the end position (end not
included).
• End: Optional. The position (up to, but not including) where to end the extraction. If
omitted, it extracts the rest of the string
• e.g. var str = 'The quick brown fox jumps over the lazy dog.';
document.write(str.slice(-9, -5));// expected output: "lazy"
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
65
MHSSP
slice()
<html>
<body>
<script>
var str = "Apple, Banana, Kiwi";
var res = str.slice(-12,-6);
document.write(res);
</script>
</body>
</html>
• Syntax: Number(object)
• The radix parameter is used to specify which numeral system to be used, for
example, a radix of 16 (hexadecimal) indicates that the number in the string
should be parsed from a hexadecimal number to a decimal number.
• Syntax: parseFloat(string)
• Syntax: number.toString(radix)
• Where:
radix: Optional. Which base to use for representing a numeric value. Must be an integer
between 2 and 36.
• 2 - The number will show as a binary value
• 8 - The number will show as an octal value
• 16 - The number will show as an hexadecimal value
• Syntax: string.toLowerCase()
• Syntax: string.toUpperCase()
• It accepts no parameter
• The index of the first character is 0, the second character 1, and so on.
• You can use the charCodeAt() method together with the length
property to return the Unicode of the last character in a string.
• The index of the last character is -1, the second last character is -2, and
so on (See Example below).
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
81
MHSSP
Unicode of the Character
• Syntax: string.charCodeAt(index)
• Where:
index: Required. A number representing the index of the character you
want to return
• This is a static method of the String object, and the syntax is always
String.fromCharCode().
• Where:
n1, n2, ..., nX: Required. One or more Unicode values to be converted
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
84
MHSSP
Character of a Unicode
<html>
<body>
<script>
function myFunction() {
var res = String.fromCharCode(65,70);
document.write(res);
}
myFunction();
</script>
</body>
</html>
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
85
MHSSP