1. Primitive - Boolean (true/ false), Number (1, 1.4), String ("Hello", 'hello'), Null (null), Undefined (undefined).
2. Non Primitive - Object (new Object()).
ES6 introduced a new type – Symbol.
The type of a variable can be determined by using the 'typeof(var_name)' function. One thing to note is that typeof(null) returns 'object'. This is a long-standing bug in JS, but one that is likely never going to be fixed. Too much code on the Web relies on the bug and thus fixing it would cause a lot more bugs!
Q. What is "use strict";? What are the advantages and disadvantages of using it?
A. If you put "use strict"; at the top of your code (or function), then the JS is evaluated in strict mode. Strict mode throws more errors and disables some features in an effort to make your code more robust, readable, and accurate.
- Strict mode helps out in a couple of ways:
- It catches some common coding bloopers, throwing exceptions.
- It prevents or throws errors when relatively “unsafe” actions are taken (such as gaining access to the global object).
- It disables features that are confusing or poorly thought out.
I had a harder time finding why people do not like strict mode. The best explanation I found was when code mixed strict and “normal” modes. If a developer used a library that was in strict mode, but the developer was used to working in normal mode, they might call some actions on the library that wouldn’t work as expected. Worse, since the developer is in normal mode, they do not have the advantages of extra errors being thrown, so the error might fail silently.
Also, as listed above, strict mode stops you from doing certain things. People generally think that you should not use those things in the first place, but some developers do not like the constraint and want to use all the features of the language.
1. Global Scope: Variables are known throughout the application, from the moment they are declared onwards.
2. Functional Scope: Variables are known within the function they are declared in, from the moment they are declared onwards.
3. Block Scope: Variables are known within the block they are declared in, from the moment they are declared onwards
How do I create block scope variables?
Traditionally, you create your variables like this :
Block scope variables are created like this :
So what is the difference between functional scope and block scope?
To understand the difference between functional scope and block scope, consider the following code :
Here, we can see that our variable j is only known in the first for loop, but not before and after. Yet, our variable i is known in the entire function from the moment it is defined onward.
Q. What is hoisting?
A. A very related question to scoping is hoisting. What do you think will be the output of this code snippet in JS :
A. When a function is created, a keyword called this is created (behind the scenes), which links to the object in which the function operates.
The this keyword’s value has nothing to do with the function itself, how the function is called determines this's value
Default "this" context
What can we expect the 'this' value to be? By default, this should always be the window Object, which refers to the root—the global scope, except if the script is running in strict mode ("use strict") this will be undefined.
What would be the this context here?...
Well, the answer is We do not know.
The this keyword’s value has nothing to do with the function itself, how the function is called determines the 'this' value.
Okay, let's change the code a bit...
Is it clearer now?
Of course, everything depends on how we call the function.
'myObject' in the code is given a property called 'myMethod', which points to the 'myMethod' function. When the 'myMethod' function is called from the global scope, this refers to the window object. When it is called as a method of 'myObject', this refers to 'myObject'.
This is called implicit binding. There is also something called explicit binding using call(), bind() or apply() which is another question.
Get ready for the second part of Front-End Developer Interview series which covers some of the most common and critical interview questions for Front-End Developers.