0% found this document useful (0 votes)
3 views

TSC-JScript

The document provides an overview of JavaScript String and Array objects, detailing their properties and methods. It includes syntax examples for creating String and Array objects, as well as explanations of methods like length, charAt, concat, and various Array methods such as pop, push, and sort. Each method is accompanied by example code snippets demonstrating their usage and expected output.

Uploaded by

a90555635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

TSC-JScript

The document provides an overview of JavaScript String and Array objects, detailing their properties and methods. It includes syntax examples for creating String and Array objects, as well as explanations of methods like length, charAt, concat, and various Array methods such as pop, push, and sort. Each method is accompanied by example code snippets demonstrating their usage and expected output.

Uploaded by

a90555635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

23.

STRING
The String object lets you work with a series of characters; it wraps Javascript's
string primitive data type with a number of helper methods.

Syntax
Use the following syntax to create a String object:

var val = new String(string);

String Properties
Here is a list of the properties of String object and their description.

Property Description

length Returns the length of the string.

In the following sections, we will have a few examples to demonstrate the usage
of String properties.

Length
This property returns the number of characters in a string.

Syntax
Use the following syntax to find the length of a string:

string.length

Return Value
Returns the number of characters in the string.

Example
137
Javascript

Try the following example.

<html>
<head>
<title>JavaScript String length Property</title>
</head>
<body>
<script type="text/javascript">
var str = new String( "This is string" );
document.write("str.length is:" +
str.length);
</script>
</body>
</html>

Output

str.length is:14

String Methods
Here is a list of the methods available in String object along with their
description.

In the following sections, we will have a few examples to demonstrate the


usage of String methods.

charAt()
charAt() is a method that returns the character from the specified index.

Characters in a string are indexed from left to right. The index of the first
character is 0, and the index of the last character in a string, called
stringName, is stringName.length – 1.

Syntax
Use the following syntax to find the character at a particular index.

string.charAt(index)

Argument Details
index: An integer between 0 and 1 less than the length of the string.
Return Value
Returns the character from the specified index.

Example
Try the following example.

<html>
<head>
<title>JavaScript String charAt() Method</title>
</head>
<body>
<script type="text/javascript">
var str = new String( "This is string" );
document.writeln("str.charAt(0) is:" + str.charAt(0));
document.writeln("<br />str.charAt(1) is:" +
str.charAt(1)); document.writeln("<br />str.charAt(2)
is:" + str.charAt(2));
t
document.writeln("<br />str.charAt(3) is:" + str.charAt(3));
document.writeln("<br />str.charAt(4) is:" + str.charAt(4));
document.writeln("<br />str.charAt(5) is:" + str.charAt(5));
</script>
</body>
</html>

Output

str.charAt(0) is:T
str.charAt(1) is:h
str.charAt(2) is:i
str.charAt(3) is:s
str.charAt(4) is:
str.charAt(5) is:i

concat ()
This method adds two or more strings and returns a new single string.

Syntax
Its syntax is as follows:

string.concat(string2, string3[, ..., stringN]);


Argument Details
string2...stringN: These are the strings to be concatenated.

Return Value
Returns a single concatenated string.

Example
Try the following example.

<html>
<head>
<title>JavaScript String concat() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is string
one" ); var str2 = new String( "This is
string two" ); var str3 =
str1.concat( str2 );

document.write("Concatenated String :" + str3);


</script>
</body>
</html>

Output

Concatenated String :This is string one This is string two

replace ()
This method finds a match between a regular expression and a string, and
replaces the matched substring with a new substring.

The replacement string can include the following special replacement patterns:

Syntax
The syntax to use the replace() method is as follows:

string.replace(regexp/substr, newSubStr/function[, flags]);


Return Value
It simply returns a new changed string.

Example
Try the following example.

<html>
<head>
<title>JavaScript String replace() Method</title>
</head>
<body>
<script type="text/javascript">

var re = “apples”;

var str = "apples are round, and apples are


juicy.";
var newstr = str.replace(“apples”, "oranges");

document.write(newstr );

</script>
</body>
</html>

Output

oranges are round, and oranges are juicy.

Search ()
This method executes the search for a match between a regular expression and
this String object.

Syntax
Its syntax is as follows:

string.search(regexp);

Argument Details
regexp : A regular expression object. If a non-RegExp object obj is passed, it is
implicitly converted to a RegExp by using new RegExp(obj).

Return Value
If successful, the search returns the index of the regular expression inside the
string. Otherwise, it returns -1.

Example
Try the following example.

<html>
<head>
<title>JavaScript String search() Method</title>
</head>
<body>
<script type="text/javascript">

var re = “apples”;
var str = "Apples are round, and apples are juicy.";

if ( str.search(re) == -1 )
{
document.write("Does not contain Apples" );
}
else
{
document.write("Contains Apples" );
}

</script>
Javascript

</body>
</html>

Output

Contains Apples

slice ()
This method extracts a section of a string and returns a new string.

Syntax
The syntax for slice() method is:

string.slice( beginslice [, endSlice] );


Argument Details
 beginSlice : The zero-based index at which to begin extraction.

 endSlice : The zero-based index at which to end extraction. If omitted, slice


extracts to the end of the string.

Return Value
If successful, slice returns the index of the regular expression inside the string.
Otherwise, it returns -1.

Example
Try the following example.

<html>
<head>
<title>JavaScript String slice() Method</title>
</head>
<body>
<script type="text/javascript">

var str = "Apples are round, and apples are juicy.";

var sliced = str.slice(3, -2);


Javascript
document.write( sliced );
</script>
</body>
</html>

Output

les are round, and apples are juic

split ()
This method splits a String object into an array of strings by separating the string
into substrings.
Syntax
Its syntax is as follows:

string.split([separator][, limit]);
Argument Details
 separator : Specifies the character to use for separating the string. If
separator is omitted, the array returned contains one element consisting of
the entire string.
 limit : Integer specifying a limit on the number of splits to be found.

Return Value
The split method returns the new array. Also, when the string is empty, split
returns an array containing one empty string, rather than an empty array.

Example
Try the following example.

<html>
<head>

<title>JavaScript String split() Method</title>


</head>
<body>
<script type="text/javascript">

var str = "Apples are round, aand apples are


juicy.";
var splitted = str.split(" ",3);
document.write( splitted );

</script>
</body>
</html>

Output

Apples,are,round,

toLowerCase ()
This method returns the calling string value converted to lowercase.

Syntax
Its syntax is as follows:

string.toLowerCase( )
Return Value
Returns the calling string value converted to lowercase.

Example
Try the following example.

<html>
Javascript
<head>
<title>JavaScript String toLowerCase() Method</title>
</head>
<body>
<script type="text/javascript">
var str = "Apples are round, and Apples are
Juicy.";
document.write(str.toLowerCase( ));
</script>
</body>
</html>

Output

apples are round, and apples are juicy.

toUpperCase ()
This method returns the calling string value converted to uppercase.

Syntax
Its syntax is as follows:

string.toUpperCase( )

Return Value

Returns a string representing the specified object.

Example

Try the following example.

<html>
<head>
<title>JavaScript String toUpperCase() Method</title>
</head>
<body>
Javascript

<script type="text/javascript">
var str = "Apples are round, and Apples are Juicy.";
document.write(str.toUpperCase( ));
</script>
</body>
</html>

Output

APPLES ARE ROUND, AND APPLES ARE JUICY.


Javascript
24. ARRAYS

The Array object lets you store multiple values in a single variable. It stores a
fixed-size sequential collection of elements of the same type. An array is used to
store a collection of data, but it is often more useful to think of an array as a
collection of variables of the same type.

Syntax
Use the following syntax to create an Array Object.

var fruits = new Array( "apple", "orange", "mango" );

The Array parameter is a list of strings or integers. When you specify a single
numeric parameter with the Array constructor, you specify the initial length of
the array. The maximum length allowed for an array is 4,294,967,295.

You can create array by simply assigning values as follows:

var fruits = [ "apple", "orange", "mango" ];

You will use ordinal numbers to access and to set values inside an array as
follows.

fruits[0] is the first element


fruits[1] is the second element
fruits[2] is the third element

Array Properties
Here is a list of the properties of the Array object along with their description.

Property Description

177
Javascript

length Reflects the number of elements in an array.

In the following sections, we will have a few examples to illustrate the usage of
Array properties.

length
Javascript array length property returns an unsigned, 32-bit integer that
specifies the number of elements in an array.

Syntax
Its syntax is as follows:

array.length

Return Value
Returns the length of an array.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array length Property</title>
</head>
<body>
<script type="text/javascript">
var arr = new Array( 10, 20, 30 );
document.write("arr.length is:" +
arr.length);
</script>
</body>
</html>

Output
arr.length is:3

Array Methods
Here is a list of the methods of the Array object along with their description.

In the following sections, we will have a few examples to demonstrate the usage
of Array methods.
concat ()
Javascript array concat() method returns a new array comprised of this array
joined with two or more arrays.

Syntax
The syntax of concat() method is as follows:

array.concat(value1, value2, ..., valueN);

Parameter Details
valueN : Arrays and/or values to concatenate to the resulting array.

Return Value
Returns the length of the array.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array concat Method</title>
</head>
<body>
<script type="text/javascript">
var alpha = ["a", "b", "c"];
var numeric = [1, 2, 3];

var alphaNumeric = alpha.concat(numeric);


document.write("alphaNumeric : " + alphaNumeric
);
</script>
</body>
</html>

Output
alphaNumeric : a,b,c,1,2,3

join ()
Javascript array join() method joins all the elements of an array into a string.

Syntax
Its syntax is as follows:

array.join(separator);

Parameter Details
separator : Specifies a string to separate each element of the array. If omitted,
the array elements are separated with a comma.

Return Value
Returns a string after joining all the array elements.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array join Method</title>
</head>
<body>
<script type="text/javascript">

var arr = new Array("First","Second","Third");

var str = arr.join();


document.write("str : " + str );

var str = arr.join(", ");


document.write("<br />str : " +
str );

var str = arr.join(" + ");


document.write("<br />str : " + str );
</script>
</body>
Javascript

</html>

Output

str : First,Second,Third
str : First, Second, Third
str : First + Second + Third

pop ()
Javascript array pop() method removes the last element from an array and returns
that element.

Syntax
Its syntax is as follows:

Array.pop();

Return Value
Returns the removed element from the array.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array pop Method</title>
</head>
<body>
<script type="text/javascript">
var numbers = [1, 4, 9];

var element = numbers.pop();


document.write("element is : " + element );
var element = numbers.pop();
document.write("<br />element is : " + element );
</script>
</body>
</html>

Output

element is : 9
element is : 4

push ()
Javascript array push() method appends the given element(s) in the last of the
array and returns the length of the new array.

Syntax
Its syntax is as follows:

Array.push();

Parameter Details
element1, ..., elementN: The elements to add to the end of the array.

Return Value
Returns the length of the new array.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array push Method</title>
</head>
<body>
<script type="text/javascript">
var numbers = new Array(1, 4, 9);
var length = numbers.push(10);
document.write("new numbers is : " + numbers );

length = numbers.push(20);
document.write("<br />new numbers is : " + numbers );
</script>
</body>
</html>

Output

new numbers is : 1,4,9,10


new numbers is : 1,4,9,10,20

reverse ()
Javascript array reverse() method reverses the element of an array. The first array
element becomes the last and the last becomes the first.

Syntax
Its syntax is as follows:

array.reverse();

Return Value
Returns the reversed single value of the array.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array reverse Method</title>
</head>
<body>
<script type="text/javascript">
var arr = [0, 1, 2, 3];
document.write("Reversed array is : " + arr.reverse() );
</script>
</body>
</html>

Output

Reversed array is : 3,2,1,0

shift ()
Javascript array shift() method removes the first element from an array and returns
that element.

Syntax
Its syntax is as follows:

array.shift();

Return Value
Returns the removed single value of the array.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array shift Method</title>
</head>
<body>
<script type="text/javascript">
var element = [105, 1, 2, 3];
document.write("Removed element is : " +
element.shift() );
</script>
</body>

</html>

Output

Removed element is : 105

slice ()
Javascript array slice() method extracts a section of an array and returns a new
array.

Syntax
Its syntax is as follows:

array.slice( begin [,end] );

Parameter Details
 begin : Zero-based index at which to begin extraction. As a negative index,
start indicates an offset from the end of the sequence.
 end : Zero-based index at which to end extraction.

Return Value
Returns the extracted array based on the passed parameters.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array slice Method</title>
</head>
<body>
<script type="text/javascript">
var arr = ["orange", "mango", "banana", "sugar", "tea"];
document.write("arr.slice( 1, 2) : " + arr.slice( 1, 2) );
document.write("<br />arr.slice( 1, 2) : " + arr.slice( 1,
3) );
</script>
</body>

</html>

Output

arr.slice( 1, 2) : mango
arr.slice( 1, 2) : mango,banana

sort ()
Javascript array sort() method sorts the elements of an array.

Syntax
Its syntax is as follows:

array.sort( compareFunction );

Parameter Details
compareFunction: Specifies a function that defines the sort order. If omitted, the
array is sorted lexicographically.
Return Value
Returns a sorted array.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array sort Method</title>
</head>
<body>
<script type="text/javascript">
var arr = new Array("orange", "mango", "banana", "sugar");

var sorted = arr.sort();


document.write("Returned string is : " + sorted );

</script>
</body>
</html>

Output

Returned string is : banana,mango,orange,sugar

toString ()
Javascript array toString() method returns a string representing the source code of
the specified array and its elements.

Syntax
Its syntax is as follows:

array.toString( );

Return Value
Returns a string representing the array.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array toString Method</title>
</head>
<body>
<script type="text/javascript">
var arr = new Array("orange", "mango", "banana", "sugar");

var str = arr.toString();


document.write("Returned string is : " + str );
</script>
</body>
</html>

Output

Returned string is : orange,mango,banana,sugar

unshift ()
Javascript array unshift() method adds one or more elements to the beginning of
an array and returns the new length of the array.

Syntax
Its syntax is as follows:

array.unshift( element1, ..., elementN );

Parameter Details
element1, ..., elementN : The elements to add to the front of the array.

Return Value
Returns the length of the new array. It returns undefined in IE browser.

Example
Try the following example.

<html>
<head>
<title>JavaScript Array unshift Method</title>
</head>
<body>
<script type="text/javascript">
var arr = new Array("orange", "mango", "banana", "sugar");

var length = arr.unshift("water");


document.write("Returned array is : " + arr );
document.write("<br /> Length of the array is : " + length
);

</script>
</body>
</html>

Output

Returned array is : water,orange,mango,banana,sugar


Length of the array is : 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy