1 of 22

JavaScript – Function

1

2 of 22

Function - Definition

  • A function is a reusable code-block that will be executed by an event, or when the function is called.

  • A function is a JavaScript procedure—a set of statements that performs a specific task.

2

3 of 22

Function - Definition

  • You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external .js file).

  • Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that the function is read/loaded by the browser before it is called, it could be wise to put it in the <head> section.

3

4 of 22

Function Definition - Syntax

  • function functionname(var1,var2,...,varX)

{

//some code(Javascript statements)

}

4

5 of 22

Function Definition

  • A function definition consists of the function keyword
  • The name of the function.
  • A list of arguments to the function, enclosed in parentheses and separated

by commas.

  • The JavaScript statements that define the function, enclosed in curly braces

{ }.

5

6 of 22

Function Definition - Syntax

  • Note:

A function with no parameters must include the parentheses () after the function name.

Do not forget about the importance of capitals in JavaScript! The word function must be written in lowercase letters, otherwise a JavaScript error occurs! Also note that you must call a function with the exact same capitals as in the function name.�

6

7 of 22

Function Definition

The return Statement:

  • This statement is used to specify the value that is returned by the function.

  • Syntax:
    • return <value>

7

8 of 22

Function calling

  • After defining the function it must be Called to perform the specified actions with the indicated parameters.

  • After it is defined a function can be called any number of times.

  • Syntax –

<function name>(value1,value2….valueN)

8

9 of 22

Example - Function Definition and calling

9

10 of 22

Example - Function Definition with arguments and calling

10

11 of 22

Example - Function Definition with arguments and return statement

11

12 of 22

JavaScript – Predefined function

  • eval
  • isFinite
  • isNaN
  • parseInt
  • parseFloat
  • escape
  • Unescape
  • String
  • Number

12

13 of 22

eval function

  • This function evaluates a string of JavaScript code without reference to a particular object.

syntax:

eval(expr)

where expr is a string to be evaluated.

  • If the string represents an expression, eval evaluates the expression.

  • If the argument represents one or more JavaScript statements, eval performs the statements.

13

14 of 22

isFinite function

  • The isFinite function evaluates an argument to determine whether it is a finite number.

  • syntax:

isFinite(number)

where number is the number to evaluate.

  • When the argument is NaN, positive infinity or negative infinity, this method returns false, otherwise it returns true.

  • Example:

if(isFinite(ClientInput) == true)

{

/* take specific steps */ }

14

15 of 22

isNaN function

  • The isNaN function evaluates an argument to determine if it is “NaN” (not a number).

  • Syntax:

isNaN(testValue)

  • isNaN returns true if passed “NaN,” and false otherwise.

15

16 of 22

parseInt function

  • It parses the argument, the string and attempts to return an integer
  • Syntax:

parseInt(str)

Note : If the first letter is a character in the given string it returns “NaN” value.

16

17 of 22

parseFloat function

  • It parses argument, the string and attempts to return an floating point value.
  • Syntax:

parseFloat(str)

Note : If the first letter is a character in the given string it returns “NaN” value.

17

18 of 22

escape function

    • This function encodes a string, so it can be read on all computers.

    • Syntax
      • escape(string)�
    • Parameter
      • stringRequired. The string to be encoded. �

18

19 of 22

escape function

    • This function encodes special characters, with the exception of: * @ - _ + . /

    • Use the unescape() function to decode strings encoded with escape().

19

20 of 22

unescape function

    • The unescape() function decodes a string encoded with escape().
    • Syntax
      • unescape(string)
    • Parameter –
      • string - The string to be decoded�
      • Note: These functions used by the server-side JavaScript to encode and decode name/value pairs in URLs.

20

21 of 22

Number function

    • This function converts the value of an object to a number.
    • Syntax
      • Number(object)
    • Parameter
      • Object - A JavaScript object
    • Note:
    • If the parameter is a Date object, the Number() function returns the number of milliseconds since midnight January 1, 1970 UTC.
    • If the object's value cannot be converted to a number, the Number() function returns NaN.

21

22 of 22

String function

    • This converts the value of an object to a string.
    • Syntax
      • String(object)
    • Parameter
      • Object - A JavaScript object�Tips and Notes
    • Note:
      • The String() function returns the same value as toString() of the individual objects.

22