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

Projector

Ky reee

Uploaded by

Krishna Thombare
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views

Projector

Ky reee

Uploaded by

Krishna Thombare
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 15
CMSI CSS 22519 Nibe Sir, CM Dept. Polytechnic, Loni Unit 2: Array, Function and String Unit 2: Array, Function and String (14 Marks) *Array: JavaScript arrays are used to store multiple values in a single variable. ‘An array is a special variable, which can hold more than one value at a time. Syntax: var array_name = |item', item2, Example var cars = ["Skoda’, "Volvo", "BMW"};

JavaScript Arrays

Example (another way to declare an array) var cars = [ "Skoda’, "Volvo", "BMW" Ik ‘Scanned with CamScanner CMSI CSS 22519 Nibe Sir, CM Dept. Polytechnic, Loni Unit 2: Array, Function and String The following example creates an Array, and assigns values to it: Example (new keyword is used) var cars = new Array("Skoda’, "Volvo", "BMW");

JavaScript Arrays

Access the Elements of an Array You access an array element by referring to the index number. This statement accesses the value of the first element in cars. var name = cars{0]; Example var cars = ["Skoda’, "Volvo", "BMW"}; document. getElementByld("demo').innerHTM& = cars(0};

JavaScript Arrays

JavaScript array elements are accessed using numeric indexes (starting from 0)

jemo">

‘Scanned with CamScanner CMSI CSS 22519 Nibe Sir, CM Dept. Polytechnic, Loni Unit 2: Array, Function and String Looping Array Elements The safest way to loop through an array, is using for loop.

JavaScript Arrays

‘Scanned with CamScanner CMSI CSS 22519 Nibe Sir, CM Dept. Polytechnic, Loni Unit 2: Array, Function and String Adding Array Elements The easiest way to add a new element to an array is using the push() method.

JavaScript Arrays

The push method appends a new element to an array.

shift() method used to delete elements of an array from first position

JavaScript Arrays

The shift method delete first element from an array.

-

‘Scanned with CamScanner CMSI CSS 22519 Nibe Sir, CM Dept. Polytechnic, Loni Unit 2: Array, Function and String splice() method used to delete range of elements from an array

JavaScript Arrays

The splice method deletes range of elements from an array.

Display specific length of an array:

JavaScript Arrays

Display given length array

Arrays and Objects In JavaScript, arrays use numbered indexes. In JavaScript, objects use named indexes. Arrays are a special kind of objects, with numbered indexes. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers. Sorting an Array The sort() method sorts an array alphabetically.

JavaScript Array Sort

The sort() method sorts an array alphabetically.

-

‘Scanned with CamScanner CMSI CSS 22519 Nibe Sir, CM Dept. Polytechnic, Loni Unit 2: Array, Function and String JavaScript Functions A JavaScript function is a block of code designed to perform a particular task A JavaScript function is executed when “something” invokes it (calls it) ‘A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parametert, parameter2, ...) The code to be executed, by the function, is placed inside curly brackets: {} function name(parametert, parameter2, parameter3) { 1 code to be executed }

JavaScript Functions

This example calls a function which performs a calculation, and returns the result:

‘Scanned with CamScanner MSI CSS 22519 Nibe Sir, CM Dept. Polytechnic, Loni Unit 2: Array, Function and String JavaScript String: String Length The length property returns the length of a string. -

JavaScript String Properties

The length property returns the length of a string:

Finding a String in a String The indexOt() method returns the index of (the position of) the first occurrence of a specified text in a string.

JavaScript String Methods

The indexOf() method returns the position of the first occurrence of a specified text:

Substring() Method: The method is used to retrieve substring using location.

JavaScript String Methods

The substring() method extract a part of a string and returns the extracted parts in a new string:

substr() Method The method is used to retrieve substring using length. n ‘Scanned with CamScanner CMSI CSS 22519 Nibe Sir, CM Dept. Polytechnic, Loni Unit 2: Array, Function and String

JavaScript String Methods

The substr() method extract a part of a string and returns the extracted parts in a new string:

Replacing String Content The replace() method replaces a specified value with another value in a string

JavaScript String Methods

Replace “Microsoft” with "W3Schools" in the paragraph below:

You might also like