A function declaration lets you create a reusable block of code that does a specific task. It helps keep your code organized and easy to understand.
Function declaration in Javascript
function functionName(parameters) { // code to run }
The function keyword starts the declaration.
The function name should be descriptive and follow naming rules (letters, numbers, _, $).
greet that prints a greeting.function greet() { console.log('Hello!'); }
add that takes two numbers and returns their sum.function add(a, b) { return a + b; }
name and prints it using a template string.function sayName(name) { console.log(`My name is ${name}`); }
This program defines a function multiply that returns the product of two numbers. It then calls the function with 4 and 5 and prints the result.
function multiply(x, y) { return x * y; } const result = multiply(4, 5); console.log('4 times 5 is', result);
You can call a function before its declaration because of how JavaScript loads functions (hoisting).
If a function does not return a value, it returns undefined by default.
Use clear names for functions to make your code easier to read.
Function declarations create named blocks of reusable code.
They help organize code and avoid repetition.
Functions can take inputs (parameters) and return outputs.
Practice
greet in JavaScript?Solution
Step 1: Recognize JavaScript function syntax
JavaScript functions are declared using the keywordfunctionfollowed by the function name and parentheses.Step 2: Check each option's syntax
function greet() { console.log('Hello'); }uses correct syntax:function greet() { ... }. Options A, B, and D use incorrect keywords or punctuation.Final Answer:
function greet() { console.log('Hello'); } -> Option AQuick Check:
JavaScript function declaration = function keyword + name + parentheses [OK]
- Using 'func' or 'def' instead of 'function'
- Adding colon after 'function'
- Missing parentheses after function name
Solution
Step 1: Identify function body syntax
In JavaScript, the function body must be enclosed in curly braces{ }.Step 2: Check each option for braces
function divide(a, b) return a / b;lacks curly braces around the return statement, causing a syntax error. Others have correct braces.Final Answer:
function divide(a, b) return a / b; -> Option CQuick Check:
Function body must be inside braces { } [OK]
- Omitting curly braces for function body
- Missing parentheses after function name
- Using incorrect keywords
function sayHi(name) {
return 'Hi, ' + name + '!';
}
console.log(sayHi('Anna'));Solution
Step 1: Understand function return and console.log
The function returns a string combining 'Hi, ', the name, and '!'. The console.log prints this returned string.Step 2: Evaluate the function call sayHi('Anna')
It returns the string 'Hi, Anna!'. console.log prints it without quotes.Final Answer:
Hi, Anna! -> Option DQuick Check:
Function returns string, console.log prints string without quotes [OK]
- Confusing returned string with printed output including quotes
- Expecting function name to print
- Ignoring return statement
function greet {
console.log('Hello!');
}Solution
Step 1: Check function declaration syntax
Functions must have parentheses after the name, even if no parameters are used.Step 2: Identify missing parentheses
The code misses parentheses aftergreet, causing a syntax error.Final Answer:
Missing parentheses after function name -> Option BQuick Check:
Function name must be followed by () [OK]
- Omitting parentheses after function name
- Thinking semicolon is required after console.log
- Confusing curly braces placement
calculateArea that takes width and height and returns the area. Which is the best correct declaration?Solution
Step 1: Understand the area formula
Area of a rectangle is width multiplied by height.Step 2: Check each function's return value
function calculateArea(width, height) { return width * height; }returns width * height, which is correct.function calculateArea(width, height) { width * height; }lacks return statement, so returns undefined. Options C and D return wrong calculations.Final Answer:
function calculateArea(width, height) { return width * height; } -> Option AQuick Check:
Return width * height for area calculation [OK]
- Forgetting return keyword
- Returning sum or division instead of product
- Not using parameters correctly
Ready to prove it?
Pass the test and this pattern moves unknown
