Skip to main content

Command Palette

Search for a command to run...

JavaScript Function

Updated
3 min readView as Markdown
JavaScript Function
R

I am a web developer. I love to build my ideas into a web.

A JavaScript function is a block of code that is designed to perform any particular task. You can execute a function by calling it. This is known as invoking or calling a function.

To use a function, you have to define it somewhere in the scope from which you wish to call it. The idea is to put some commonly performed task together and make a function so that instead of writing the same code again and again for different inputs, we can call that particular function.

The basic syntax to create a function in JavaScript is as follows:

function functionName(paramiter1,paramiter2,paramiter3.........)

{//function body}

JavaScript consists of various built-in or predefined functions. But, it also allows us to create user-defined functions. So let’s move ahead and have a look at some of the commonly used predefined functions.

FunctionsDescription
EvalEvaluates a string/arithmetic expression and returns a value.
ParseIntParses a string argument and returns an integer of the specified base.
ParseFloatParses a string argument and returns a floating-point number.
EscapeReturns the hexadecimal encoding of an argument.
UnescapeReturns the ASCII string for the specified value.

Let’s take an example and see how these predefined functions work in JavaScript:

var x = 10;
var y = 20;
var a = eval("x * y")  // Eval
var b = parseInt("10.00")  // ParseInt
var c = parseFloat("10")  // ParseFloat
escape("Welcome to Edureka") // Escape
unescape("Welcome to Edureka") // Unescape

Different ways to define JavaScript Function

Function Methods - javascript function - edureka

A function can be defined using various ways. It is important to check how the function interacts with the external components and the invocation type. The different ways include:

Function Declaration

A function declaration consists of a function keyword, an obligatory function name, a list of parameters in a pair of parenthesis and a pair of curly braces that delimits the body code.

It is defined as:

// function declaration
function isEven(num) {
return num % 2 === 0;
}
isEven(24); // => true
isEven(11); // => false

Function isEven(num) is a function declaration that is used to determine if a number is even.

Related Article: JS Interview Questions

Function Expression

A function expression is determined by a function keyword, followed by an optional function name, a list of parameters in a pair of parenthesis and a pair of curly braces that delimits the body code.

It is defined as:

const count = function(array) { // Function expression
return array.length;
}
const methods = {
numbers: [2, 5, 8],
sum: function() { // Function expression
return this.numbers.reduce(function(acc, num) { // func. expression
return acc + num;
});
}
}
count([1, 7, 2]); // => 3
methods.sum(); // => 15

The function expression creates a function object that can be used in various situations like:

  • It can be assigned to a variable as an object: count = function(…) {…}

  • Create a method on an object sum: function() {…}

  • Use the function as a callback: .reduce(function(…) {…})