Functions in Javascript

Functions

To define functions we have the instruction function

After this reserved word is placed the name of the function followed by a list of arguments delimited by parentheses and separated by commas

The return statement

The statement return it is the one that allows you to return the result of a function

The example shows a function that returns the area of a square of side l

Properties of the functions

JavaScript associates two properties with each function:

  • arguments
    it allows the management of the optional parameters
  • caller
    identifies the function that made the call

The property arguments

Is an array that contains the parameters that are passed to the function

In the example we have defined the Sum function to calculate the sum of the numbers passed as arguments

Thus, Sum(4,5,7) returns 16 and Sum(56) returns 56

The caller property

Displays the name of the calling function, so this property will return a string of characters

Considerations to be taken into account

Before you start working with features, you need to consider the following points:

  • The JavaScript language does not allow the definitions of nested functions

  • The passing of parameters is done by value

    That is, if a function modifies the content of its arguments, this modification is local and does not affect either globally or the function made the call

Functions are predefined by the language

Eval function

Has as argument an expression and returns the value of the same

This function is useful to evaluate a string of characters that represents a numeric expression

The issue was effected through a form field is a string of characters that it is sometimes necessary to convert to a numeric value

The following example illustrates how allowing the user to enter a numeric expression you can visualize it as the value of the expression

If you haven't entered anything, you see undefined and if it's not a numerical expression, it doesn't make changes to the visualization


Functions escape and unescape

These two functions allow you to encode strings in URL format

This encoding is necessary in the automatic creation of hypertext links or in the definition of persistent properties as the Cookies

Function isNaN

Function that checks if the value passed for the parameter is numeric or not

The result of this function is a Boolean

That is, it evaluates an argument to see if it is NaN: (Not a Number)


Function parseFloat

Converts a string of characters to a floating-point number

If a non number character is found, the sign '+', '-' or exponent, returns the value found up to that point

Similarly, if the first character cannot be converted to number it will return zero


Function parseInt

Converts a string of characters to an integer number with a specified base

La base puede ser 8, 10 ó 16

If a non number character is found, the sign '+', '-' or exponent, returns the value found up to that point

Similarly, if the first character cannot be converted to number it will return zero