-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReturnValue.js
More file actions
29 lines (25 loc) · 1010 Bytes
/
ReturnValue.js
File metadata and controls
29 lines (25 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Every function returns a value, which by default is undefined.
Any function is terminated when its lines of code end, or when the execution flow finds a return keyword.
When JavaScript encounters this keyword it exits the function execution and gives control back to its caller.
You can only return one value.
*/
//If I pass a value, that value is returned as the result of the function:
let dosomething = () => {
return 'testing'
}
let result = dosomething() // result === 'test'
console.log(result)
/* To simulate returning multiple values, you can return an object literal, or an array, and use a destructuring
assignment when calling the function. */
//returning multiple values using arrays
let simulateArray = () => {
return ['Carl', 17]
}
let [name, age] = simulateArray()
console.log(name, age)
//returning multiple values using objects
let simulateArray2 = () => {
return {name2: 'Lisa', age2: 33}
}
let {name2, age2} = simulateArray2()
console.log(`${name2} is ${age2} years old.`)