Java-Scripting Nots

You might also like

Download as pdf
Download as pdf
You are on page 1of 44
‘é > <= an 12 tior Array, Func cee Se Sete ne ou | ; : ; co | ¢ function - Calin 5 : aaion, rea fing . wCodeAth), } 0 ipulste a string, Joining a string, retriev a 24 Array Sapien penal wast Mer sw sore An array is an object that can store a collection of Items. Arrays is U ot ee wi have to create 300 gal data of the same type lke storing of details of 100 students. If you a a vay by referring to its des al Fe aan hs orem ssepe ay rove terri Ta Numbers, Strings and Boolean ina single eray. 2.1.1 Declaring an Array There are basically two ways to declare an array 1. By array literal : syntax : (7 var areayname= [value valued, sovalueN a Values are contained inside []and separated by, (comma). Program 2.1.1 : Write a Javascript code to demonstrate Array Literal Solution : [ Stitle> Array Demo | EE ee eee on iis | Array Demo - Intemet Explore? aD (nl | th of Array =5 | | a | f 2. By creating instance of Array : @.__Whatie Aray ? How to create Array in Javascript. ¥ } Syntax new keyword is used to stance of array Program 2.1.2 ; Write a Javascript code to create an Array and print its length. Solution [enemi> iS " [ | Array Demo extjavascript”> |. [ | Array Demo | = "Javascript" type="text/javascript" > |Sbody> { j l ‘Output : B) avaScriptiartishtrnl =] #9 fray Demo el all elements from array in ascending order, we can use reverse method t 1. _reverse() : sort method will sor e manner i.e. in descending order. sorted list in revers Syntax : array.reverse\): the reversed sin jt will retur Program 2.1.10 Here, separator is a string lke /-;" tc to separate each element of the array. If separator is not mentioned, i wil SBI ist with comma as separator. This funciontetuma esting uher plang cl Santen ge Program 2.1.11: Witte a Javascript code fo combine anay elements Ino tng without separate Solution : FB) EVavaScriptiarii.htmi >| \*#! | Search x @ Array Demo After joining =C,C++Java wr ‘array, Eungtion and | vmbine an afray elements into stings using #2 Pregram 2.1.12: Wate a Javascript code o solution : ers [ |stite> Ary Dane ‘ e | BS = | body> | [ } [ 2.2.4 Scope of Variable and Argument [.@. Explain scope of variables declared in function with example 4. Global Scope : All the variables that you declare, is by default defined in global scope. When a variable Is declared outside a function with a var keyword then that variable i a global variable because its available through all part of script. 2. Local Scope : All variable which are declared using var keyword within function then those variables are known as local variables and they are accessible within function only. Program 2.2.3 : Write a Javascript code to demonstrate scope of variable, Solution : Stitle> Function Demo | | ecrptlanguage="vascript” iype="texjavascrpt= fare: Welcome"; _// Globally declared variable Function display() it {var !="Thanks"; {Locally declared variable t.write("Global Value document.write) } [anol 0 | [ [bonis = { {hemt> ce re Output : @ Function Demo x (a Global Value =Welcome Local Value =Thanks 2.3 Calling a Function 2.3.1 Calling a Function with or without Argument | [ @__How to call function from HTML tag, expiain with example. | {In Javascript function calling is by using name of function followed by parenthesis. ‘Syntax : [finction namep: Program 2.3.1 : Write a Javascript code to call user define function. [ | [ Output : Function Demo - Internet Explorer -\o) x) ; IAJavaScript\arr3.html +4 [Search Function Demo Area of Rectangle =15 If function has arguments (parameters) then values for each argument in placed within parenthesis separeted by commas ‘Syntax : function _name(values_for_parameters): g program 2.2.2: Write a Javascript code io demonstrate funstion cling with arguments, Solution : Function Demo 1" type= +myString.in eee aes 2 Internet Explorer alot st) on Seg oe e tml =] #9. | Search. (na q [ao] 1JavaScript\stel ht String Demo Position = this method searches and returns the index number of last occurrence of the character/substing Me ig. Searches the string from end to beginning. = ____ ad iaexOfloubat, (start) ie The value of start can be between 0 to the length of string, Solution a | | I Stitle > String Demo lefiucad> |< seript language="Javascript” type="textjavaseript"> "Hello World's har myString ng.indexOf"l"))s document, write("First Position ="+myS "+myString.lastIndexOf("l")); ldocument,write(”
Last Positis |jscript> Is head > fbody > 4 Output 5 Internet Explo er String Demo thant 1 Leh | Semen First Position = | Last Position =9 (ii) Search() : This method searches the string fo “ring for @ specified value and returns the position ofthe if match found. Syntax : [string.search (word) Program 2.4.8: Write a Javascript code to implement search) metho of sting Solution nas ' l< head> |etine> String Demo ‘ Result =Icome to Jay aScript Result =Icome 2.4.7 Converting String to Numbers and Numbers to String {0 _arsaint(): The parselnt() parses a string and returns a whole numb Program 2.4.12 : Welt a Javascript code io conver ating Wo iioper Naabe Solution Stitle>String Demo text/javascript"> [