Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f73b432

Browse files
committed
new js questions added
1 parent e44c51a commit f73b432

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

β€ŽREADME.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Prepare for your next 2023 JavaScript interview with these tricky
44
githubPath: "https://github.com/Vasu7389/JavaScript-Interview-Questions-2023"
55
---
66

7-
<span style=" font-size: 1rem; border-bottom: 1px solid grey;"> Updated Apr 16, 2023 </span>
7+
<span style=" font-size: 1rem; border-bottom: 1px solid grey;"> Updated May 19, 2023 </span>
88

99
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).
1010

@@ -1018,3 +1018,96 @@ Since a and b reference the same array, both console.log(a) and console.log(b) w
10181018
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.
10191019

10201020
</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(typeof 42);
1028+
console.log(typeof "Hello");
1029+
console.log(typeof true);
1030+
console.log(typeof [1, 2, 3]);
1031+
console.log(typeof { name: "John", age: 25 });
1032+
console.log(typeof null);
1033+
console.log(typeof undefined);
1034+
console.log(typeof function () {});
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.
1081+
```
1082+
1083+
</summary>
1084+
Answer:
1085+
1086+
```js
1087+
const getType = (val) => (val === null ? null : val?.constructor.name);
1088+
```
1089+
1090+
The output of the code will be:
1091+
1092+
```bash
1093+
number
1094+
string
1095+
boolean
1096+
Array
1097+
Object
1098+
null
1099+
undefined
1100+
Function
1101+
```
1102+
1103+
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

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /