Learn JavaScript Functions
JavaScript Developer WorldWide
Join the JavaScript Developers worldwide JavaScript Developers JavaScript Coders JavaScript Freelancers
Coding Exercises on JavaScript Functions
Basic Function Definition and Invocation
Task: Write a JavaScript function named greet that prints "Hello, World!" to the console.
Purpose: Learn how to define and call a simple function.
Sample Code:
function greet() {
????console.log("Hello, World!");
}
greet(); // Function invocation
Explanation:
This exercise demonstrates the basic structure of a function in JavaScript. The greet function is defined using the function keyword followed by the name of the function. It encapsulates a console log statement and is called (invoked) by its name followed by parentheses.
Function with Parameters
Task: Define a function named add that takes two parameters and returns their sum.
Purpose: Practice writing functions that take parameters and understand how to return a value.
Sample Code:
function add(a, b) {
????return a + b;
}
console.log(add(5, 3)); // Outputs 8
Explanation:
The add function demonstrates functions with parameters and a return statement. It receives two arguments, a and b, adds them, and returns the sum. The return statement sends this value back to where the function was called.
Default Parameters
Task: Create a function named multiply that multiplies two numbers. Assign a default value of 1 to both parameters.
Purpose: Understand default parameter values in functions.
Sample Code:
function multiply(a = 1, b = 1) {
????return a * b;
}
console.log(multiply(4)); // Outputs 4
console.log(multiply(4, 5)); // Outputs 20
Explanation:
This function utilizes default parameters. If the function is called with less than two arguments, the missing arguments are assigned a default value of 1. This prevents errors and allows the function to operate with fewer inputs.
Scope of Variables Inside Functions
Task: Demonstrate the scope of a variable by defining a variable inside a function and trying to access it outside the function.
Purpose: Explore the concept of local scope within functions.
Sample Code:
function showNumber() {
????let num = 10; // Local variable
????console.log(num); // Accessible here
}
showNumber();
console.log(num); // Error: num is not defined
Explanation:
The variable num is defined inside the showNumber function, making it local to that function. It cannot be accessed outside the function, illustrating the concept of local scope.
Function Expression
Task: Write a function expression that calculates the square of a number and use it immediately after.
Purpose: Understand function expressions and how they differ from function declarations.
Sample Code:
领英推荐
const square = function(num) {
????return num * num;
};
console.log(square(4)); // Outputs 16
Explanation:
This exercise shows a function expression where the function is assigned to the variable square. Unlike function declarations, function expressions are not hoisted, meaning they cannot be called before they are defined in the code.
Multiple Choice Quiz Questions
What will happen if you try to call a function before it is defined in JavaScript?
A) It will throw an error.
B) It will return undefined.
C) It will work if it's a function declaration.
D) It will work only if it's a function expression.
Correct Answer: C) It will work if it's a function declaration
Explanation: Function declarations are hoisted in JavaScript, which means they are moved to the top of their containing scope during the compile phase, allowing them to be called before they are defined.
What is a correct example of a function with default parameters?
A) function multiply(a, b = 1) { return a * b; }
B) function multiply(a = 1, b) { return a * b; }
C) function multiply(a, b) { return a * b = 1; }
D) function multiply(a = 1, b) { return a * b = 1; }
Correct Answer: A) function multiply(a, b = 1) { return a * b; }
Explanation: This is the correct way to set default parameters in JavaScript functions. The second parameter b is given a default value of 1.
What does the following function return?
function checkNum(num) {
????if (num > 10) {
????????return true;
????}
}
console.log(checkNum(8));
A) true
B) false
C) undefined
D) 0
Correct Answer: C) undefined
Explanation: Since the condition num > 10 is not met and there is no else part, the function returns undefined for any input less than or equal to 10.
Which statement about the scope of variables in JavaScript is true?
A) Variables defined inside a function are accessible globally.
B) Variables defined with var inside a function are local to the function.
C) Variables defined with let inside a function can be accessed anywhere in the script.
D) Global variables can only be accessed inside functions.
Correct Answer: B) Variables defined with var inside a function are local to the function
Explanation: Variables declared with var inside a function have local scope to that function, meaning they can only be accessed within that function.
Which is an example of a function expression in JavaScript?
A) function add(a, b) { return a + b; }
B) const subtract = function(a, b) { return a - b; };
C) subtract(a, b) { return a - b; }
D) function multiply(a, b);
Correct Answer: B) const subtract = function(a, b) { return a - b; };
Explanation: A function expression involves defining a function and assigning it to a variable, as shown in option B. Unlike function declarations, function expressions are not hoisted.