This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathexercise.js
More file actions
58 lines (40 loc) · 1.22 KB
/
exercise.js
File metadata and controls
58 lines (40 loc) · 1.22 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
By now, you would have already seen "undefined", either in an error message or being output from your program.
But what does it mean? undefined represents the absence of a value.
In some cases, undefined will be used by a programmer intentionally, and they will write code to handle it.
But usually, when you see undefined - it means something has gone wrong!
Below are 4 typical examples of when you would see undefined.
For each example, can you explain why we are seeing undefined?
*/
// Example 1
let a;
console.log(a);
//because "a" did not assign to any value
// let a = 2;
// console.log(a)
// Example 2
function sayHello() {
let message = "Hello";
}
//I think because 'message' is declared but its value is never read.
// function sayHello() {
// let message = "Hello";
// return message
// }
let hello = sayHello();
console.log(hello);
// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}
sayHelloToUser();
// any returned value
// function sayHelloToUser(user) {
// let userName = user
// console.log(`Hello ${userName}`);
// return `Hello ${userName}`
// }
// Example 4
let arr = [1, 2, 3];
console.log(arr[3]);
//non-existing array element