
Scope determines the availability or accessibility or visibility of variables, objects, and functions.
There are mainly two type of scope in javascript
- Global Scope
- Local Scope
- Block Scope
- Function Scope
The difference between let and var is of Scoping.
- let gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlike var.
- var is rather a keyword which defines a variable globally regardless of block scope.
The both are very similar when used like the following, outside a function block.
Window Object: However, global variables defined with let will not be added as properties on the global window object.
Block Scope: let has block level scope.
Redeclaration: let and const variables cannot be re-declared while var variable can be re-declared in the same scope.
Let's read more about - const in JavaScript ES6
A function expression can be stored in a variable and the variable can be used as a function
There are many benefits of using function expressions
- It can be used as Closure
- It can be passed as arguments to other Function
- It cab be used as Immediately Invoked Function Expressions (IIFE)
Function Expressions can not be Hoisted.
- An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.
- It is a design pattern which is also known as a Self-Executing Anonymous Function
Example
- Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).
- variables and function declarations are moved to the top of their scope before code execution.
- In JavaScript, a variable can be declared after it has been used. In other words, a variable can be used before it has been declared.
JavaScript only hoists declarations, not initializations.
You may also like - Javascript ES6 Interview Questions
Yes, JavaScript is case sensitive.
1. Anonymous Function - This type of function has no name.
2. Regular Named Function - This function is named properly and specifically.
3. Arrow Function - Arrow function (kind of anonymous function) is part of ES6.
Arrow functions is best suited with anything that requires this to be bound to the context, and not the function itself.
- Shorter Syntax
- No binding of this
Unlike a regular function, an arrow function does not bind this. Instead, this is bound lexically (i.e. this keeps it's meaning from its original context).
An arrow function does not have its own this. this value of the enclosing lexical scope is used by arrow functions. So if this is not present in current scope, an arrow function ends up looking into lexical scope.
You may also like - React.js Interview Questions
Object is an unordered collection of data in the form of key:value pairs.
- A JavaScript object is an entity having State and Behavior (Properties and Methods).
- JavaScript is a prototype based language, so everything is an Object.
There are many ways to define or create an object in JavaScript.
- Object Literals
- Object Constructor
- Object.create Method
- using Function
- using Class
ES6 introduced the class keyword to create classes in JavaScript.
Object.create() method creates a new object, using an existing object as the prototype of the newly created object.
Primitives data types like strings and numbers are compared by their value, while objects like arrays, dates, and plain objects are compared by their reference.
The comparison by reference basically checks to see if the objects given refer to the same location in memory.
A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function's variables.
- It has access to it's own scope i.e. variables defined between its curly brackets.
- It has access to the outer function's variables.
- It has access to the global variables.
Reason: It will reference the last value stored in i, which was 5.
There are two solution to resolve this problem
- Closure
- let
Closure It creates a unique scope for each iteration, storing each unique value of the variable within it's scope.
Reason: let has block level scope so variable i is only seen in the for loop's block scope.
A callback function is a function passed into another function as an argument(let’s call this other function "otherFunction"), and the callback function gets executed inside the otherFunction after the function has finished execution.
JavaScript is an event driven language. This means that instead of waiting for a response before moving on, JavaScript will keep executing while listening for other events.
Callbacks are a way to make sure certain code doesn't execute until other code has already finished execution.
You may also like - Node.js Interview Questions and Answers
In JavaScript, objects have a special hidden property [[Prototype]] (as named in the specification), that is either null or references another object. That object is called "Prototype".
That [[Prototype]] has a "magical" meaning. When we want to read a property from object, and it’s missing, JavaScript automatically takes it from the prototype. In programming, such thing is called "prototypal inheritance"
The property [[Prototype]] is internal and hidden, but there are many ways to set it. via __proto__or Object.getPrototypeOf
// old-way
obj.__proto__
// new-way
Object.getPrototypeOf(obj)
The __proto__ is considered outdated and somewhat deprecated (in browser-only part of the Javascript standard).
- Object.create(proto[, descriptors]) – creates an empty object with given proto as [[Prototype]] and optional property descriptors.
- Object.getPrototypeOf(obj) – returns the [[Prototype]] of obj.
- Object.setPrototypeOf(obj, proto) – sets the [[Prototype]] of obj to proto.
Classical Inheritance: A constructor function instantiates an instance via the new keyword. This new instance inherits properties from a parent class.
Prototypal Inheritance: An instance is created by cloning an existing object that serves as a prototype. This instance—often instantiated using a factory function or "Object.create()"—can benefit from selective inheritance from many different objects.
A promise is an object which can be returned synchronously from an asynchronous function.
Promise overcome the problem of callback hell.
- Fulfilled: onFulfilled() will be called (e.g. resolve() was called).
- Rejected: onRejected() will be called (e.g. reject() was called).
- Pending: initial state, neither fulfilled nor rejected.
The apply() method calls a function with a given this value, and arguments provided as an array.
Syntax: fun.apply(thisArg, [argsArray])
The call() method calls a function with a given this value and arguments provided individually
Syntax: fun.call(thisArg[, arg1[, arg2[, ...]]])
A useful mnemonic is "A for array and C for comma."
bind allows you to set the this value now while allowing you to execute the function in the future, because it returns a new function object.
Syntax: fun.bind(thisArg[, arg1[, arg2[, ...]]])
The spread operator (…) is a new operator introduced in ES6. It allows the expansion of an iterable (e.g., Array) into its constituent elements.
- Using ES6 - Spread & Set Operator
- Using jQuery each function
Set was introduced in ES6: They can't have duplicate Items.
After we have done that, we simply convert the Set back to an array by using the spread operator.
Following are the advantages of using JavaScript:
- Lightweight: JavaScript can be executed within the user’s browser without having to communicate with the server, saving on bandwidth.
- Speed: Client-side JavaScript is very fast because it can be run immediately within the client-side browser.
- Simplicity: JavaScript is relatively simple to learn and implement.
- Versatile: JavaScript supports multiple programming paradigms—object-oriented, imperative, and functional programming and can be used on both front-end and server-side technologies.
- Interactivity: Because tasks can be completed within the browser without communicating with the server, JavaScript can create a smooth "desktop-like" experience for the end user.
- Rich Interfaces: From drag-and-drop blocks to stylized sliders, there are numerous ways that JavaScript can be used to enhance a website’s UI/UX.
- Prototypal Inheritance: Objects can inherit from other objects, which makes JavaScript so simple, powerful, and great for dynamic applications.
- Platform Independent: JavaScript is platform independed language. Any JavaScript-enabled browser can understand and interpreted JavaScript code.
Design patterns are advanced object-oriented solutions to commonly occurring software problems.
As the name suggests, these patterns are for handling object creational mechanisms.
- Constructor pattern
- Factory pattern
- Prototype pattern
- Singleton pattern
This pattern helps to obtain new functionalities without tampering with the existing ones.
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy
These patterns are concerned with improving communication between dissimilar objects
- Iterator pattern
- Observer pattern
- Template pattern
- Strategy pattern
- State pattern
- Command pattern
No, JavaScript does not support overloading!. JavaScript supports overriding not overloading.
If you will define two or more functions with the same name, the last one will override the previously defined functions and when a call will be made to the function, the last defined function will get executed.
Yes, it's true. since null is a primitive datatype still typeof null return object.
This is a kind of bug in javascript which can't be fixed.
In the inital release of JavaScript, values were represented as a combination of
- Tag Type
- Actual Value
tag refering for object and null was same, that's why typeof null return object.