You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<spanstyle="font-size: 1rem; border-bottom: 1pxsolidgrey;"> Updated May 19, 2023 </span>
8
8
9
9
In this article, we will cover a range of JavaScript interview questions, including those related to the latest versions of the language (ES6, ES7, ES8, and ES9).
10
10
@@ -1018,3 +1018,96 @@ Since a and b reference the same array, both console.log(a) and console.log(b) w
1018
1018
This is different from the previous example where ... spread operator was used, which created a new array with the same values as the original array instead of referencing the same array.
1019
1019
1020
1020
</details>
1021
+
1022
+
<details>
1023
+
<summary>
1024
+
<h3>34. Guess the output of the following code that uses the typeof operator.</h3>
1025
+
1026
+
```js
1027
+
console.log(typeof42);
1028
+
console.log(typeof"Hello");
1029
+
console.log(typeoftrue);
1030
+
console.log(typeof [1, 2, 3]);
1031
+
console.log(typeof { name:"John", age:25 });
1032
+
console.log(typeofnull);
1033
+
console.log(typeofundefined);
1034
+
console.log(typeoffunction () {});
1035
+
```
1036
+
1037
+
</summary>
1038
+
Answer:
1039
+
1040
+
```bash
1041
+
bash
1042
+
Copy code
1043
+
number
1044
+
string
1045
+
boolean
1046
+
object
1047
+
object
1048
+
object
1049
+
undefined
1050
+
function
1051
+
```
1052
+
1053
+
The typeof operator in JavaScript is used to determine the type of a value or expression. Here's the breakdown of the output:
1054
+
1055
+
- typeof 42 returns "number" because 42 is a numeric value.
1056
+
- typeof "Hello" returns "string" because "Hello" is a string.
1057
+
- typeof true returns "boolean" because true is a boolean value.
1058
+
- typeof [1, 2, 3] returns "object" because arrays are considered objects in JavaScript.
1059
+
- typeof { name: "John", age: 25 } returns "object" because objects are considered objects in JavaScript.
1060
+
- typeof null returns "object", which is a known quirk in JavaScript. null is considered an object type.
1061
+
- typeof undefined returns "undefined" because it is a special value in JavaScript representing an uninitialized variable.
1062
+
- typeof function() {} returns "function" because it is a function object.
1063
+
1064
+
</details>
1065
+
1066
+
<details>
1067
+
<summary>
1068
+
<h3>35. Write a function in JavaScript to determine the type of a value.</h3>
1069
+
1070
+
```javascript
1071
+
console.log(getType(42));
1072
+
console.log(getType("Hello"));
1073
+
console.log(getType(true));
1074
+
console.log(getType([1, 2, 3]));
1075
+
console.log(getType({ name:"John", age:25 }));
1076
+
console.log(getType(null));
1077
+
console.log(getType(undefined));
1078
+
console.log(getType(function () {}));
1079
+
1080
+
//The function should print "array" for "[]" and "null" for "null" types.
The function getType takes a value as an argument and returns its type. If the value is null, it returns null. Otherwise, it uses the constructor.name property to determine the type of the value.
1104
+
1105
+
- getType(42) returns "number" because 42 is a numeric value.
1106
+
- getType("Hello") returns "string" because "Hello" is a string.
1107
+
- getType(true) returns "boolean" because true is a boolean value.
1108
+
- getType([1, 2, 3]) returns "Array" because arrays are considered objects in JavaScript, and the constructor name for an array is "Array".
1109
+
- getType({ name: "John", age: 25 }) returns "Object" because objects are considered objects in JavaScript, and the constructor name for an object is "Object".
1110
+
- getType(null) returns null because null is a special value in JavaScript.
1111
+
- getType(undefined) returns "undefined" because it is a special value in JavaScript representing an uninitialized variable.
1112
+
- getType(function() {}) returns "Function" because it is a function object, and the constructor name for a function is "Function".
1113
+
- The getType function can be used to dynamically determine the type of values in JavaScript.
0 commit comments