Which of the following is a valid way to define a function in JavaScript?
A
function = myFunc() {} B
function myFunc() {} C
define myFunc() {} D
func myFunc() {}
Analysis & Theory
`function myFunc() {}` is the correct way to define a named function.
What is the output of the following code?
```js
function greet() { return 'Hello'; }
console.log(greet());
```
A
Hello B
undefined C
greet D
Error
Analysis & Theory
Calling `greet()` returns `'Hello'`, which is logged to the console.
How do you define an anonymous function and assign it to a variable?
A
let x = function() {} B
let x = () => function {} C
function = x() {} D
anonymous function x()
Analysis & Theory
`let x = function() {}` defines an anonymous function assigned to the variable `x`.
What is a function expression?
A
A function defined using the `function` keyword without a name B
A loop inside a function C
A method attached to a class D
A function defined inside HTML
Analysis & Theory
A function expression is a function defined and assigned to a variable, often anonymous.
Which of the following defines a function using arrow syntax?
A
let greet => () 'Hello'; B
function => greet() { return 'Hello'; } C
let greet = () => 'Hello'; D
arrow greet() { return 'Hello'; }
Analysis & Theory
Arrow functions are defined using `=>`. `let greet = () => 'Hello';` is valid.
What will this code print?
```js
function add(a, b) {
return a + b;
}
console.log(add(3, 4));
```
A
34 B
7 C
`add` D
`undefined`
Analysis & Theory
The function returns the sum of `a` and `b`, which is `7`.
What is the default return value of a function that does not explicitly return anything?
A
`null` B
`0` C
`undefined` D
`false`
Analysis & Theory
Functions without a return statement return `undefined` by default.
Can functions be passed as arguments to other functions in JavaScript?
A
Yes B
No C
Only in ES6 D
Only inside objects
Analysis & Theory
Functions are first-class objects in JavaScript and can be passed as arguments.
How do you define a function with parameters?
A
function myFunc = param1, param2 {} B
function myFunc(param1, param2) {} C
function(myFunc[param1, param2]) {} D
let myFunc(param1 param2) = {}
Analysis & Theory
Function parameters go inside parentheses: `function myFunc(param1, param2) {}`
Which of these is NOT a valid way to define a function?
A
`function sum() {}` B
`let sum = function() {}` C
`let sum = () => {}` D
`define sum() {}`
Analysis & Theory
`define` is not a keyword in JavaScript for creating functions.