0% found this document useful (0 votes)
32 views13 pages

Javascript 5

This document contains 3 paragraphs that summarize JavaScript arrays and strings: 1) Arrays allow storing multiple values using indexes, and can be initialized using an array literal with values separated by commas in brackets or the Array constructor. 2) Variables declared within a function have local scope only accessible in that function, while variables declared outside have global scope accessible everywhere. 3) Strings are objects representing character sequences, created using string literals in quotes or the String constructor, and various methods manipulate strings like charAt(), concat(), toLowerCase(), etc.

Uploaded by

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

Javascript 5

This document contains 3 paragraphs that summarize JavaScript arrays and strings: 1) Arrays allow storing multiple values using indexes, and can be initialized using an array literal with values separated by commas in brackets or the Array constructor. 2) Variables declared within a function have local scope only accessible in that function, while variables declared outside have global scope accessible everywhere. 3) Strings are objects representing character sequences, created using string literals in quotes or the String constructor, and various methods manipulate strings like charAt(), concat(), toLowerCase(), etc.

Uploaded by

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

1

Web Programming
JAVASCRIPT
BY:
Abid jameel
Email: [email protected]

2
Js Arrays
 We have learned that a variable can hold only one value, for
example var i = 1, we can assign only one literal value to i. We cannot
assign multiple literal values to a variable i. To overcome this problem,
JavaScript provides an array.

 An array is a special type of variable, which can store multiple values


using special syntax. Every value is associated with numeric index
starting with 0. The following figure illustrates how an array stores
values.

3
Array Initialization:
 An array in JavaScript can be defined and initialized in two ways,
 array literal and
 Array constructor syntax.
Array Literal:
Array literal syntax is simple. It takes a list of values separated by a
comma and enclosed in square brackets.
var arrayname = [element0, element1, element2,... elementN];

Example:
<p id=“testing"></p>
<script>
var stringArray = ["one", "two", "three"];
document.getElementById(“testing").innerHTML = stringArray;
</script>

NOTE: JavaScript array can store multiple element of different data


types. It is not required to store value of same data type in an array.

4
Array Constructor:
You can initialize an array with Array constructor syntax
using new keyword.
<p id="p1"></p>
<script>
var stringArray = new Array();
stringArray[0] = "one";
stringArray[1] = "two";
stringArray[2] = "three";
stringArray[3] = "four";
document.getElementById("p1").innerHTML = stringArray;
<script>

Accessing Array Elements:

<p id="p1"></p>
<script>
var stringArray = new Array("one", "two", "three", "four");
document.getElementById("p1").innerHTML = stringArray[0];
</script>
5
Scope in JavaScript:
 Scope in JavaScript defines accessibility of variables, objects and functions.
 There are two types of scope in JavaScript.
 Global scope
 Local scope
 Local JavaScript Variables:
 Local variables have local scope, They can only be accessed within the
function.
<button onclick="myFunction()">Click</button>
<script>
function myFunction()
{
var x =10;
var y = 20;
var result= x+y;
document.write(result);
}

</script>
6
Global JavaScript Variables
A variable declared outside a function, becomes GLOBAL.
A global variable has global scope: All scripts and
functions on a web page can access it.
<p id="testing"></p>
<script>
var friend = "Shani";
function myFunction()
{
document.getElementById("testing").innerHTML =
"My friend name is " + friend;
}
myFunction();
</script>
 Automatically call Global variables
friend = “shani”;
If you assign a value to a variable that has not been declared, it will automatically become
a GLOBAL variable

7
JavaScript Strings
 The JavaScript string is an object that represents a
sequence of characters.
 A string can be any text inside quotes. You can use
single or double quotes.
Example: var myName= “developer";
 There are 2 ways to create string in JavaScript
 By string literal
 By string object (using new keyword)
o string literal:
o <script>
var str="This is string literal";
document.write(str);
</script>

8
 string object (using new keyword):
syntax:
var stringname=new String("string literal");
Code Example:
<script>
var stringname = new String("hello javascript string");
document.write(stringname);
</script>

9
JavaScript String Methods
Let's see some of the JavaScript string methods .
 charAt(index)
 concat(str)
 indexOf(str)
 lastIndexOf(str)
 toLowerCase()
 toUpperCase()
 slice(beginIndex, endIndex)
 trim()
charAt(index):
<script>
var str="javascript";
document.write(str.charAt(2));
</script>
10
concat(str) Method
<script>
var s1="javascript ";
var s2="concat example";
var s3=s1.concat(s2);
document.write(s3);
</script>
toLowerCase() Method:
<script>
var s1="JavaScript toLowerCase Example";
var s2=s1.toLowerCase();
document.write(s2);
</script>
toUpperCase() Method:
<script>
var s1="JavaScript toUpperCase Example";
var s2=s1.toUpperCase();
document.write(s2);
</script>
11
12
13

You might also like