Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Functions in JS

JavaScript functions are defined with the function keyword. We may use a function declaration or a function expression.

Function Declaration:

We declare functions with the following Syntax.

function functionName(parameters) {
// code which we have be execut.
}

Declared functions are not executed immediately. Instead, they are saved for later use and executed later when invoked (called upon). Let us see below given an example to understand it better:

Example:
function myFunction(a, b) {
return a + b;
}
var z = myFunction(7, 5);
Here 12 will be stored in z as it is calling function to add 7 and 5.

Function Expressions:

A JavaScript function can also be defined using an expression. A function expression can be stored in a variable. After a function expression has been stored in a variable, the variable can be used as a function. Let us see below given an example to understand it better:

Example:
var x = function (a, b) {return a + b};
var z = x(7, 5);
Here 12 will be stored in z as it is calling function stored in x to add 7 and 5.

Please note the function above is actually an anonymous function (a function without a name). Functions stored in variables do not need function names. They are always invoked using the variable name.

Functions by new Keyword:

Functions can also be defined with a built-in JavaScript function constructor called Function(). Let us see below given an example to understand it better:

Example:
var x = new Function("a", "b", "return a + b");
var z = x(7, 5);
This is similar to using var x = function (a, b) {return a + b}; So here again 12 will be stored in z.

Function Parameters and Arguments:

Function parameters are the names listed in the function definition. Function arguments are the real values passed to (and received by) the function. JavaScript function definitions do not specify data types for parameters. Also, JavaScript functions do not perform type checking on the passed arguments, and the number of arguments received.

Example:
function functionName(parameter1, parameter2, parameter3) {
// code which we will be executed
}

If a function is called with missing arguments (less than declared), the missing values are set to undefined.

Try the following example in the editor below.

You are given three numbers. You have to implement one function which will return the product of those numbers. Please note that you don’t have to print values, implement the function which takes three parameters and return the product as mentioned in the editor below.

Start solving Functions in JS on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test