You are on page 1of 27
Basics of Javascript Programming Syllabus + 1,1. Features of JavaScript 4.2 Object Name, Property, M 4.3 Values and Variables 44 Operators and Expressions- Primary Expressions, Object and Array initializers, expression, property access expressions, invocation expressions. 1.8 if Statement, if... else, if..elseif, nested if statement. 1.6 Switch ... case statement 4.7 Loop statement - for loop, Querying and setting properties and deleting properties , properly getters and setters jethod, Dot syntax, Main event. function definition for...in loop, while loop, do... while loop, continue statement. 18 1.1 What is JavaScript Programming ? _ JavaScript is popular, light weight and an Open Source Client Side Language supported by all Browsers — JavaScript is a dynamic computer programming language. When Javascript is applied with an HTML document, it can provide dynamic interactivity on website. JavaScript is also being used in many Game development and Mobile Application development. ie Server JavaScript Fig. 1.1.1: Client side scripting {ts lightweight and most commonly used as a part of web pages, whose implementations allow client-side script to interact with the user and make dynamic pages. - tis an interpreted programming language with object-oriented capabilities, 1.1.1 Features of JavaScript Q. Enlist features of JavaScript. - JavaScript is an object-based scripting language. Scanned wth C3 ‘Scanned wih CamScanner ‘Scanned wih CamScanner {nthe above code we have declare 3 properties using key-value pairs ~ ~ First property is name with value “Vijay” ~ Second property is age with value 22 ~ Third property year with value "TY" {tls possible to access the properties of objects by two ways -student[‘age"] or student.age 1.2.3 Method Amethod is a set of instructions performed by an object when it receives a message. (On the form when you click a Submit button, the form is submitted to the server-side application. It means, clicking the ‘Submit button causes the button to process a some set of instructions (method). ‘The kinds of methods that are used differ, depending on the type of object to which they're attached. Program 1.2.2 : Write a JavaScript code toilustrate the use of method. ‘Scanned wih CamScanner ‘Scanned wih CamScanner Scanned with CamScanner ‘Scanned wih CamScanner 6. _Bitwise Operators : Bit operators work on 32 bits numbers. Any numeric operand in 3 ee ete te te 8piret corber, canned with CamSeanner eawwrite(XOR =" = § > 1)); cwrite(""); 9 : -write("Left Shift"+ (x = 5 << 1)); swrite(’
"}; ent.write("Right Shift =" +.(x = 5. >> 1)); 1.4.2 Expressions Primary expressions : = Primary expressions are the simplest expressions which do not include any simpler expressions. ~ in it, the primary expressions are constant or literal values, keywords, and variable references. Example : Number Literal “Welcome” | String Literal Ipattern/ _| Regular Expression Literal Se ‘Scanned wih CamScanner Program 1.8.3 Wrne « Javascript cose to demenavate he wae ft etme? statement document. writeione + "is lows than” + two 4°"), } eden { document. writrione + "is grater thao” + } “ < bent > 5 haal: tee + ois ‘Scanned wih CamScanner +54 Nested it Statement of another if oF else, Nested if statements af staremeot that is the target. Acnested ifs a ‘means an if statement inside ailows us to nest if statements within if statements. ie, ‘Yes, JavaScript: ani staneement, ‘we can place an if statement inside ‘Scanned wih CemScanner HG < a); | document write ‘is atoaller than 20 "; J ebne{ | Hocument.writer is ‘reater than 20"); ) WEY” [O2) EVavaScripthevenchtmt =f c @ Nested t Statment x(a tee 9 iis 10 148 smaller than 20 1.6 Switch Case Statement ipt is used for decision maki branch statement. It provides an easy way to execution different i Ind Purposes. The switch case statement isa arts of code b2sed on the value of the expres muttinat sion. ‘Scanned wih CemScanner 1-23 statement]; ie case valué2: statement2; break; ‘case valueN: statement; break; default: eds Program 1.6.1 : Write a code to illustrate use of Switch case in Javascript. Solution : Switch Case Output : -1235 <5 Exit from loop ..- stops the execution of a loop entirely. The break statement Program 1.7.5: Develop a Javascript code 10 Hlustrato use of bresk statement rogram 1.7.5: Solution : var i while(i<5) it ists ‘Scanned with CemScanner 1.8 Querying, Setting Properties and Deleting Properties 1.8.1 Querying and Setting Properties The dot (. ) operator or square brackets ([}) are used to obatined values of properties, var price = book.price; // Get the "price" property of the book. var name = person.lastname // Get the “lastname” property of the person, #8 the dot operator, the right-hand must be2 The left-hand side should be an expression whose value is an object. If usin simple identifier that names the property. If using square brackets, the value within the brackets mus bean expression that eva luates to a string that contains the desired property name var author = book{"author"] // Get the "author" property of the book, Use dot or sauare brackets ene OF St 2 OPEN tu ace the ropes to the letand side ‘ Of an assignme expression: book price = 300; // Create an “price” property of book. ["FirstName"] = "thon; // Set the "FirstName" property, person! used Ithen the expression inside the the square bracket notation are $QUare bracke te ust evaluate to a string. eo ‘Scanned wih CamScanner ‘7h Glee Nevwrors wed i dete beth the rave of he pragerty and poogerty sel (Aeon pean sg: 1) The gence stiec: mom has ne age seem Gente person Firsmiame”t // The perver cnet has oe Fertbeme Sroperty The Setete property rena true # deleted succentuity a Sonsion reccar et cote: “Reed”, termed: Ford” document. verneicarcoler + "* + cx heand + ""); Accum weitedebete car bead), Output : ‘Scanned wih CamScanner ° Accessed, the return value from the getter is used- Bet ~ a functi Sn without arguments, that works when a property i ead Program a2; ‘82: Wee a code is Mlustrate the use of getter method in Javascript. Solution : i ~ “Script language="javascript type="textjavascript"> var car = { color: "Red", brand : “Ford”, get company() { return this.brand; } bs ’ Display data from the object using a getter: document.write("Company ="+car.company); ‘ se Output : Nope ee aie ack 'Gc-B EWavaScriptigetterhtmi =] 45) |B wNaascriptigeternmt x [iif Company =Ford. Lo x} Search ‘Scanned wih CamScanner Basics of Javascript Programmin Lang 433 ‘When a value is set, the setter is called and passed the value that was Set © Seta function with one argument, that is called when the property is set Program 1.8.3 : Write a code to illustrate the use of setter method in Javascript. Solution : =e "= "javascript" type="text/javascript”> W Create an abject: lvar car = { color: "Red, brand : "Ford", ‘set company(value) { this.brand=value; } Hs |document.write("Company ="+car.brand +"
"}; Jear.company="Maruti"; | Display data from the object using a getter: ldocument.write("Company ="+car.brand); [eft > Output Oo: - WaaSciptseterhiml =] + [Search @ WoavaScriptsetter ttt —_x | Company =Ford Company =Maruti ‘Scanned wih ComScanner

You might also like