Skip to content

Commit 3d1827b

Browse files
committed
tricky questions
1 parent c33bd34 commit 3d1827b

File tree

2 files changed

+107
-46
lines changed

2 files changed

+107
-46
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
##### Question - What is a potential pitfall with using typeof bar === "object" to determine if bar is an object? How can this pitfall be avoided?
2+
3+
#### Answer
4+
5+
Although `typeof bar === "object"` is a reliable way of checking if bar is an object, the surprising gotcha in JavaScript is that null is also considered an object!
6+
7+
Therefore, the following code will, to the surprise of most developers, log true (not false) to the console:
8+
9+
```
10+
var bar = null;
11+
console.log(typeof bar === "object"); // logs true!
12+
```
13+
14+
As long as one is aware of this, the problem can easily be avoided by also checking if bar is null:
15+
16+
```
17+
console.log((bar !== null) && (typeof bar === "object")); // logs false
18+
```
19+
20+
To be entirely thorough in our answer, there are two other things worth noting:
21+
22+
First, the above solution will return false if bar is a function. In most cases, this is the desired behavior, but in situations where you want to also return true for functions, you could amend the above solution to be:
23+
24+
```
25+
console.log((bar !== null) && ((typeof bar === "object") || (typeof bar === "function")));
26+
```
27+
28+
Second, the above solution will return true if bar is an array (e.g., if var bar = [];). In most cases, this is the desired behavior, since arrays are indeed objects, but in situations where you want to also false for arrays, you could amend the above solution to be:
29+
30+
```
31+
console.log((bar !== null) && (typeof bar === "object") && (toString.call(bar) !== "[object Array]"));
32+
```
33+
34+
However, there’s one other alternative that returns false for nulls, arrays, and functions, but true for objects:
35+
36+
```
37+
console.log((bar !== null) && (bar.constructor === Object));
38+
```
39+
40+
Or, if you’re using jQuery:
41+
42+
```
43+
console.log((bar !== null) && (typeof bar === "object") && (! $.isArray(bar)));
44+
45+
```
46+
47+
ES5 makes the array case quite simple, including its own null check:
48+
49+
```
50+
console.log(Array.isArray(bar));
51+
```

README.md

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,57 @@
44

55
A collection of super-popular Interview questions, along with explanations and implementation examples that I was putting together for myself while preparing for my first Full-Stack JavaScript job interviews.
66

7-
## Most common Interview Topics and Questions along with explanations (Link across this whole Repository)
7+
## Table of Contents of this Readme file
8+
9+
1. [Understanding the Theory and the fundamentals of some super-popular Algorithm questions](#understanding-the-theory-and-the-fundamentals-of-some-super-popular-algorithm-questions)
10+
11+
2. [Github Repositories with large collections of problems-and-solutions of them most popular Interview challenges](#github-repositories-with-large-collections-of-problems-and-solutions-of-them-most-popular-interview-challenges)
12+
13+
3. [Overall multi-factor approach for winning this huge challenge and a great journey of getting the first Developer Job](#overall-multi-factor-approach-for-winning-this-huge-challenge-and-a-great-journey-of-getting-the-first-developer-job)
14+
15+
4. [Other important resources](#other-important-resources)
16+
17+
5. [Coding Challenge Practice Platforms](#coding-challenge-practice-platforms)
18+
19+
6. [More curated list of general resources for JavaScript Interviews](#more-curated-list-of-general-resources-for-javascript-interviews)
20+
21+
7. [Most frequently asked concepts for Front End Engineering Interview](#most-frequently-asked-concepts-for-front-end-engineering-interview)
22+
23+
8. [List of sites where you can hunt for a developer job](#list-of-sites-where-you-can-hunt-for-a-developer-job)
24+
25+
9. [Want a startup job?](#want-a-startup-job)
26+
27+
10. [Best places to job hunt for remote jobs](#best-places-to-job-hunt-for-remote-jobs)
28+
29+
11. [Here are a few places to hunt for ios, react, vue and more](#here-are-a-few-places-to-hunt-for-ios-react-vue-and-more)
30+
31+
12. [Want a list of just JavaScript jobs?](#want-a-list-of-just-javascript-jobs)
32+
33+
13. [Are you looking for a junior dev job?](#are-you-looking-for-a-junior-dev-job)
34+
35+
14. [Women focused job boards!](#women-focused-job-boards)
36+
37+
15. [Want a job as a freelance dev? Here's a list](#want-a-job-as-a-freelance-dev-heres-a-list)
38+
39+
16. [Some useful websites for programmers](#some-useful-websites-for-programmers)
40+
41+
17. [When you get stuck](#when-you-get-stuck)
42+
43+
18. [For small project ideas](for-small-project-ideas)
44+
45+
19. [General Coding advice](general-coding-advice)
46+
47+
20. [Coding Style](#coding-style)
48+
49+
21. [General Good Articles](#general-good-articles)
50+
51+
22. [Collection of Leetcode Problem solution](#collection-of-leetcode-problem-solution)
52+
53+
23. [Collection of Cracking the Coding Interview Book Problem solution](#collection-of-cracking-the-coding-interview-book-problem-solution)
54+
55+
## Most common Fundamental JavaScript Interview Topics & Questions
56+
57+
(along with explanations - Links are across this whole Repository)
858

959
- [Explain event delegation](Javascript/event-delegation-propagation-bubbling.md)
1060
- [Explain how `this` works in JavaScript](Javascript/this-keyword/this-keyword-2nd-example-GREAT-Example.md)
@@ -81,54 +131,14 @@ A collection of super-popular Interview questions, along with explanations and i
81131
- [truthy-falsy-2](Javascript/truthy-falsy-2.js)
82132
- [truthy-falsy-pass-by-value-vs-reference-strict-equality-use-case](Javascript/truthy-falsy-pass-by-value-vs-reference-strict-equality-use-case.js)
83133
- [undefined-vs-not_defined](Javascript/undefined-vs-not_defined.md)
134+
- [Why-eval-function-considered-dangerous](Javascript/Why-eval-function-considered-dangerous.md)
135+
- [pitfall-of-using-typeof](Javascript/Tricky-JS-Problems/pitfall-of-using-typeof.md)
84136

85-
## Table of Contents of this Readme file
86-
87-
1. [Understanding the Theory and the fundamentals of some super-popular Algorithm questions](#understanding-the-theory-and-the-fundamentals-of-some-super-popular-algorithm-questions)
88-
89-
2. [Github Repositories with large collections of problems-and-solutions of them most popular Interview challenges](#github-repositories-with-large-collections-of-problems-and-solutions-of-them-most-popular-interview-challenges)
90-
91-
3. [Overall multi-factor approach for winning this huge challenge and a great journey of getting the first Developer Job](#overall-multi-factor-approach-for-winning-this-huge-challenge-and-a-great-journey-of-getting-the-first-developer-job)
92-
93-
4. [Other important resources](#other-important-resources)
94-
95-
5. [Coding Challenge Practice Platforms](#coding-challenge-practice-platforms)
96-
97-
6. [More curated list of general resources for JavaScript Interviews](#more-curated-list-of-general-resources-for-javascript-interviews)
98-
99-
7. [Most frequently asked concepts for Front End Engineering Interview](#most-frequently-asked-concepts-for-front-end-engineering-interview)
100-
101-
8. [List of sites where you can hunt for a developer job](#list-of-sites-where-you-can-hunt-for-a-developer-job)
102-
103-
9. [Want a startup job?](#want-a-startup-job)
137+
## Most common Node Interview Topics & Questions
104138

105-
10. [Best places to job hunt for remote jobs](#best-places-to-job-hunt-for-remote-jobs)
106-
107-
11. [Here are a few places to hunt for ios, react, vue and more](#here-are-a-few-places-to-hunt-for-ios-react-vue-and-more)
139+
(along with explanations - Links are across this whole Repository)
108140

109-
12. [Want a list of just JavaScript jobs?](#want-a-list-of-just-javascript-jobs)
110-
111-
13. [Are you looking for a junior dev job?](#are-you-looking-for-a-junior-dev-job)
112-
113-
14. [Women focused job boards!](#women-focused-job-boards)
114-
115-
15. [Want a job as a freelance dev? Here's a list](#want-a-job-as-a-freelance-dev-heres-a-list)
116-
117-
16. [Some useful websites for programmers](#some-useful-websites-for-programmers)
118-
119-
17. [When you get stuck](#when-you-get-stuck)
120-
121-
18. [For small project ideas](for-small-project-ideas)
122-
123-
19. [General Coding advice](general-coding-advice)
124-
125-
20. [Coding Style](#coding-style)
126-
127-
21. [General Good Articles](#general-good-articles)
128-
129-
22. [Collection of Leetcode Problem solution](#collection-of-leetcode-problem-solution)
130-
131-
23. [Collection of Cracking the Coding Interview Book Problem solution](#collection-of-cracking-the-coding-interview-book-problem-solution)
141+
- [Why-eval-function-considered-dangerous](Javascript/Why-eval-function-considered-dangerous.md)
132142

133143
## Understanding the Theory and the fundamentals of some super-popular Algorithm questions
134144

0 commit comments

Comments
 (0)