MOTOSHARE 🚗🏍️

Rent Bikes & Cars Directly from Owners

Motoshare connects vehicle owners with people who need bikes and cars on rent. Owners earn from idle vehicles, and renters get flexible ride options.

Visit Motoshare

How to Write Function in JavaScript?

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.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x