How to Write Function in JavaScript?

Function

Function are subprograms which are used to compute a value or perform a task.

Type of Function

Library or Built-in functions

Ex: – valueOf( ) , write( ), alert( ) etc

– User-defined functions

Creating and Calling a Function

Creating a Function

Syntax: –

function function_name( )
{
Block of statement;
}

Calling a Function

Syntax: –
function_name( );

Ex: –
display( );

Rules of Function

  • Function name only starts with a letter, an underscore ( _ ).
  • Function name cannot start with a number.
  • Do not use reserved keywords. e.g. else, if etc.
  • Function names are case-sensitive in JavaScript.

How Function Call Works

The code inside a function isn‟t executed until that function is called.

Function with Parameters

Syntax: –
function function_name (parameter1, parameter2, ….)
{
Block of statement;
}

  • JavaScript function definitions do not specify data types for parameters.
  • JavaScript functions do not perform type checking on the passed arguments.
  • JavaScript functions do not check the number of arguments received.

Call Function with Parameter

Syntax: –
function function_name (para1, para2, ….)
{
Block of statement;
}
Syntax:-
function_name(argument1, argument2);

Function Argument Missing

If a function is called with missing arguments, the missing values are set to undefined.

Arguments Object

The arguments object contains an array of the arguments used when the function was called.
This object contains an entry for each argument passed to the function, the first entry’s index starting at 0. The argument’s object is not an Array. It is similar to an Array but does not have any Array properties except length.

Many Function Arguments

If a function is called with too many arguments, these arguments can be reached using the arguments object which is a built-in.

Tagged : / / /

Difference between Function and Method in JavaScript

So what’s the different between the two?

  • Methods have a “receiver” while functions do not.
  • Functions and methods are the same in JavaScript, but a method is a function, which is a property of an object.
  • Syntax for calling/ definition/declaration of functions and methods are different.
  • A function can be called directly by its name but methods are called by its object name and its method name using dot notation or square bracket notation.
  • Function passes the data and methods operates the data passed in the class.
  • A function lives on its own but method is a function associated with an object property.
  • Data passed to a function is explicit but method implicitly passes the object on which it was called.

Lets see detailed difference between them:-

A function in JavaScript is similar to a procedure —>a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output

Few Important things of functions:-

  • The function is executed when something calls/invokes it.
  • The name may contain letters, digits, dollar signs, underscore.
  • Parameters are listed inside round parenthesis after the name of the function.
  • Arguments are values a function receives when it is invoked.
  • When the control reaches the return statement, js will stop executing and the value is returned to the caller.

There are three steps of Functions as well as Methods to use in the program.

  1. Declaration
  2. Definition.
  3. Call

In JavaScript function declaration and definition are mostly done together.

A JavaScript method is a property of an object that contains a function definition. Methods are functions stored as object properties.

Few Important things of Methods:-

  • In JavaScript, however, a method is a function that belongs to an object.
  • JavaScript methods are the actions that can be performed on objects.
  • A JavaScript method is a property containing a function definition.
  • Accessing the object method is done with the following syntax: objectName.methodName()

Syntax of Function

var func = function(a, b) {
    var sum = a + b;
    return sum;
}
  
console.log(sum(1, 2));

Syntax of Methods

object = {
    methodName: function() {
        // Content
    }
};

object.methodName()

Few important links for learning:-

  1. Let’s see an example for User form validation in JavaScript. click here.
  2. Let’s understand JavaScript. what and how? click here.
  3. Want to know what is function and how to use them in JavaScript? Just click here.
  4. Want to know what are variables and how to use them in JavaScript? Just click here.

Tagged : / / / /

Predefined Functions in JavaScript

Want to know what is function and how to use them in JavaScript? Just click here.

Every computer programming language has functions. Some functions are defined by the programmers while the code is being written while some functions are predefined when the language was built.

As every functions have three parts

  1. Function Declaration
  2. Function Definition.
  3. Function Call

In predefined functions, the first two points are already done inside the library which we include as a header file at the beginning of the program. we just have to call the function.

Few most common predefined functions that we use are:-

Functions to display something in JavaScript:-

alert(message– It displays an alert box with a message specified in it.

confirm(message) – It will display the message with two buttons. “OK” and “Cancel”.

prompt(message) – Displays a box with the message passed. There the user can enter text in the prompt field, and choose OK or Cancel. If the user chooses OK, the string value entered in the field is returned and If the user chooses Cancel, a NULL value is returned.

Functions for conversion in JavaScript:-

parseFloat() – It returns floating-point numbers the same way as the parseInt function does, but looks for floating-point qualified strings and returns their value as afloat.

parseInt()– It converts a string to an integer returning the first integer encountered which is contained in the string. If no integer values are found such as in the string “written text”, then a value of 0 is returned.

Functions for comparison in JavaScript:-

eval()– Converts a string to integer or float value.

isNaN(value) – If the value passed is not a number, the boolean value of true is returned, if it is a number, it returns false.

escape(string) – It passes the string to be encoded and returns the encoded string. Encodes a string from ASCII into an ISO Latin-1 (ISO 8859-1) character set for HTML processing.

unescape() – Converts an ISO8859-1 character set to ASCII.

isFinite() – function determines whether the passed value is a finite number. If needed, the parameter is first converted to a number.

Functions for decoding and encoding in JavaScript:-

decodeURI() function decodes a Uniform Resource Identifier (URI) previously created by encodeURI or by a similar routine. decodeURIComponent()

decodeURIComponent() method decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent() or by a similar routine. encodeURI

encodeURI() encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two “surrogate” characters). encodeURIComponent()

encodeURIComponent() encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two “surrogate” characters).

There are more predefined functions but these were the important ones which are often used.

Few important links for learning:-

  1. Let’s see an example for User form validation in JavaScript. click here.
  2. Let’s understand javascript. what and how? click here.
  3. Want to know what is function and how to use them in JavaScript? Just click here.
  4. Want to know what are variables and how to use them in JavaScript? Just click here.
Tagged : / / / /