The document discusses JavaScript functions and objects. It explains that functions allow code to be bundled and reused, and functions in JavaScript link web page actions to code. It provides examples of built-in functions like eval(), parseInt(), parseFloat(), and dialog functions like alert(), prompt(), and confirm(). It also covers JavaScript objects, how to create and instantiate objects, and standard objects like Date, Math, and String with their properties and methods.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9K views
Functions
The document discusses JavaScript functions and objects. It explains that functions allow code to be bundled and reused, and functions in JavaScript link web page actions to code. It provides examples of built-in functions like eval(), parseInt(), parseFloat(), and dialog functions like alert(), prompt(), and confirm(). It also covers JavaScript objects, how to create and instantiate objects, and standard objects like Date, Math, and String with their properties and methods.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27
Shubhangi Shinde
A function is a block of code that has a name.
Way for bundling several commands together Tool to organize your code. User can write his own functions JavaScript functions is to link actions on a web page with the JavaScript code. JavaScript has some in-built functions. To create a function you define its name, any values ("arguments"), and some statements: function myfunction(argument1,argument2,etc) { some statements; } eval Evaluates a string of JavaScript code without reference to a particular object. Syntax eval( string) This function evaluates a string of JavaScript code without reference to a particular object e.g.: var ivalue = eval( 10*5+5) ivalue will be assigned the value 55 after the execution of this statement. parseInt and parseFloat Return a numeric value when given a string as an argument. Syntax parseInt( string) , Syntax parseFloat( string) parseInt() Converts a string value to an integer. parseInt() returns the first integer contained in a string . If the string does not begin with an integer, NaN (not a number) is returned. e.g.: var inum=parseInt( 1abc) // returns 1 var inum= parseInt(abc) // returns NaN parseFloat( ) - The parseFloat method returns the first floating point number contained in string passed. If the srint does not begin with a floating point number, NaN (not a number) is returned. e.g: var fnum=parseFloat(3.14abc) // returns 3.14 var fnum= parseFloat(abc) // returns NaN isNaN( ) - Returns true if the argument passed is not a number or false otherwise. Evaluates an argument to determine if it is NaN (not a number). Syntax isNaN( testValue) isFinite evaluates an argument to determine whether it is a finite number Syntax isFinite( number) The isFinite function evaluates an argument to determine whether it is a finite number. The syntax of isFinite is: isFinite( number), where number is the number to evaluate. If the argument is NaN, positive infinity or negative infinity, this method returns false, otherwise it returns true.
The Number and String functions let you convert an object to a number or a string. The syntax of these functions is: Number( objRef) OR String( objRef) where objRef is an object reference. The following example converts the Date object to a readable string. D = new Date (430054663215) // The following returns x = String(D) // "Thu Aug 18 04:37:43 GMT- 0700 (Pacific Daylight Time) 1983" Infinity Infinity is a numeric value representing infinity NaN NaN is a value representing Not-ANumber. undefined undefined is the value undefined. There are three different types of dialog boxes which are the methods of window object. All these three dialog boxes are modal i.e. the user must close it before continuing. Alert dialog box - alert( message) Takes in a string argument and displays an alert box. alert ( ) - Displays a dialog box with the message string passed to the alert() method and an OK button. Syntax: alert ( message ) e.g.: alert(Click OK to continue) Prompt dialog box prompt(message[, inputDefault]) Displays a message and a data entry field. prompt ( ) - Displays a Prompt dialog box with a message and an input field. Returns the value the user types in. Syntax: prompt(message, default value) e.g.: prompt (Enter your age, 25) Serves as a technique for confirming user actions. confirm() - The confirm dialog box lets you ask the user a "yes-or-no" question, and gives the user the option of clicking either an OK button or a Cancel button. The confirm method returns either true or false. Syntax: confirm (message) e.g.: confirm (Click OK to continue; Click cancel to exit) JavaScript is based on object-based paradigm. An object is a construct with properties that are JavaScript variables or other objects. An object also has functions associated with it that are known as the objects methods. JavaScript provides support for both standard and user-defined objects. Creating object types Instantiating objects of both standard and user-defined object types You can define the composition of the object in terms of other information. You can add and delete properties from an existing object. Some amount of polymorphism - the arguments array can be used for function definition. Using keyword new. Use new as follows: objectName = new objectType ( param1 [, param2] ...[, paramN] ) To instantiate a standard object type var dtToday = new Date(); where Date() is an inbuilt object. Once instantiated, the properties and methods of the object can be used accordingly. Example var newDateObj = new Date(); alert(Today Date is : + newDateObj.getDate()); alert(And the Month is : + newDateObj.getMonth()); The Date object is used to work with dates and times. Constructors new Date(); -returns the current system date new Date( milliseconds) 1 January 1970 00:00:00. new Date( yr_num, mo_num, day_num[, hr_num, min_num, sec_num, ms_num]) getMinutes() : to get the minutes from the Date object getDay() : returns the day of the week i.e. value 0 (Sunday) to 6 (Saturday) setTime() : sets the time of the date object getTime() : returns the number of milliseconds since January 1, The built-in Math object includes mathematical constants and functions You do not need to create the Math object before using it. Methods/ properties Math.abs(number) Math.sin(number) Math.exp(number) Math.log(number) Math.max(num1,num2) Math.sqrt(number) Math.random() Math.round(x) Math.pow(x,y) Math.ceil(x) Math.floor(x) Math.PI - Math.E The String object is a wrapper around the string primitive data type. A string literal and string object is different. s1 = "foo" //creates a string literal value s2 = new String("foo") //creates a String object You can call any of the methods of the String object on a string literal valueJavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the String.length property with a string literal. You should use string literals unless you specifically need to use a String object, because String objects can have counterintuitive behavior. For Eg: s1 = "2 + 2" //creates a string literal value s2 = new String("2 + 2") //creates a String object eval(s1) //returns the number 4 eval(s2) //returns the string "2 + 2" A String object has one property, length, that indicates the number of characters in the string. myString = "Hello, World!" x = mystring.length //assigns x the value 13 A String object has two types of methods: those that return a variation on the string itself, such as substring and toUpperCase, and those that return anHTML-formatted version of the string, such as bold and link. e.g: mystring.toUpperCase( ) //return the string HELLO, WORLD!. mystring.link(http://www.helloworld.com) // create a hyperlink <html> <head> <Script language="JavaScript"> document.write(' Test'.indexOf('T')); document.write('This '.lastIndexOf('T')); document.write('This is test'.charAt(10)); document.write('This program Test'.length); document.write(' program'.substring(1, 10)); document.write('my exam Test'.substr(7, 10)); document.write('my program'.toUpperCase()); document.write(' first JavaScript program'.toLowerCase()); document.write("<br />") </script> <body> </body> </html>