What Are The Pros And Cons Of Promises Over Callbacks
Below are the list of pros and cons of promises over callbacks,
Q27 Explain The Procedure Of Document Loading
- Search for all the properties, provided to the object.
- Involve all the property values, used in the content that is being served for the page about to load.
To load the document instantly, it is a great practice to add the < script> tag inside the < body> tag. The below program loads the document automatically and returns the OS details of the user:
encodeURI function is used to encode an URL. This function requires a URL string as a parameter and return that encoded string.decodeURI function is used to decode an URL. This function requires an encoded URL string as parameter and return that decoded string.
Note: If you want to encode characters such as / ? : @ & = + $ # then you need to use encodeURIComponent.
leturi="employeeDetails?name=john& occupation=manager" letencoded_uri=encodeURI letdecoded_uri=decodeURI
Also Check: How To Call And Set Up An Interview
Which One Is Not A Difference Between Typeof And Instanceof
- typeof returns a type and instanceof returns a boolean.
- instanceof requires TypeScript and typeof does not.
- typeof takes the variable name on the right, and instanceof takes a value instead of on the left and right.
What Is A Service Worker
A Service worker is basically a script that runs in the background, separate from a web page and provides features that don’t need a web page or user interaction. Some of the major features of service workers are Rich offline experiences, periodic background syncs, push notifications, intercept and handle network requests and programmatically managing a cache of responses.
Recommended Reading: How To Pass Software Engineer Interview
How Do You Trim A String At The Beginning Or Ending
The trim method of string prototype is used to trim on both sides of a string. But if you want to trim especially at the beginning or ending of the string then you can use trimStart/trimLeft and trimEnd/trimRight methods. Let’s see an example of these methods on a greeting message,
vargreeting=" Hello, Goodmorning! " console.log // " Hello, Goodmorning! "console.log) // "Hello, Goodmorning! "console.log) // "Hello, Goodmorning! "console.log) // " Hello, Goodmorning!"console.log) // " Hello, Goodmorning!"
- Const The idea of const functions is not allow them to modify the object on which they are called. When a function is declared as const, it can be called on any type of object.
- Let It is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block its defined in.
What Is A Void Operator
The void operator evaluates the given expression and then returns undefined. The syntax would be as below,
Let’s display a message without any redirection or reload
Note: This operator is often used to obtain the undefined primitive value, using “void”.
Give An Example Where Do You Really Need Semicolon
// define a functionvarfn=))
and it will be interpreted as
In this case, we are passing the second function as an argument to the first function and then trying to call the result of the first function call as a function. Hence, the second function will fail with a “… is not a function” error at runtime.
Also Check: What To Ask Your Interviewer
What Are The Modules In Nodejs
Node.js has many modules to provide the basic functionality needed for a web application. Some of them include:
Includes classes, methods, and events to create a Node.js HTTP server
Includes utility functions useful for developers
Includes events, classes, and methods to deal with file I/O operations
Includes methods for URL parsing
Includes methods to work with query string
Includes methods to handle streaming data
Includes methods to compress or decompress files
What Is A Proper Tail Call
First, we should know about tail call before talking about “Proper Tail Call”. A tail call is a subroutine or function call performed as the final action of a calling function. Whereas Proper tail call is a technique where the program or code will not create additional stack frames for a recursion when the function call is a tail call.
For example, the below classic or head recursion of factorial function relies on stack for each step. Each step need to be processed upto n * factorial
functionfactorialreturnn*factorial }console.log) //120
But if you use Tail recursion functions, they keep passing all the necessary data it needs down the recursion without relying on the stack.
functionfactorialreturnfactorial }console.log) //120
The above pattern returns the same output as the first one. But the accumulator keeps track of total as an argument without using stack memory on recursive calls.
You May Like: How To Get Interviewed On A Podcast
Question #: Using A Closure Within A Loop
Closures are sometimes brought up in an interview so that the interviewer can gauge how familiar you are with the language, and whether you know when to implement a closure.
A closure is basically when an inner function has access to variables outside of its scope. Closures can be used for things like implementing privacy and creating function factories. A common interview question regarding the use of closures is something like this:
Write a function that will loop through a list of integers and print the index of each element after a 3 second delay.
A common implementation Ive seen for this problem looks something like this:
If you run this youll see that you actually get the 4 printed out every time instead of the expected 0, 1, 2, 3 after a 3 second delay.
The reason for this is because the setTimeout function creates a function that has access to its outer scope, which is the loop that contains the index i. After 3 seconds go by, the function is executed and it prints out the value of i, which at the end of the loop is at 4 because it cycles through 0, 1, 2, 3, 4 and the loop finally stops at 4.
What Is The Use Of History Object
The history object of a browser can be used to switch to history pages such as back and forward from the current page or another page. There are three methods of history object.
Recommended Reading: How To Master An Interview
The Indeed Editorial Team comprises a diverse and talented team of writers, researchers and subject matter experts equipped with Indeed’s data and insights to deliver useful tips to help guide your career journey.
How Do You Reuse Information Across Service Worker Restarts
The problem with service worker is that it gets terminated when not in use, and restarted when it’s next needed, so you cannot rely on global state within a service worker’s onfetch and onmessage handlers. In this case, service workers will have access to IndexedDB API in order to persist and reuse across restarts.
You May Like: What To Say In An Interview Thank You Card
Q: Explain The Difference Between Objectfreeze Vs Const
const and Object.freeze are two completely different things.
- const applies to bindings . It creates an immutable binding, i.e. you cannot assign a new value to the binding.
const person = let animal = person = animal // ERROR "person" is read-only
- Object.freeze works on values, and more specifically, object values. It makes an object immutable, i.e. you cannot change its properties.
let person = let animal = Object.freeze person.name = "Lima" //TypeError: Cannot assign to read only property 'name' of objectconsole.log
How Do You Create Specific Number Of Copies Of A String
The repeat method is used to construct and return a new string which contains the specified number of copies of the string on which it was called, concatenated together. Remember that this method has been added to the ECMAScript 2015 specification.Let’s take an example of Hello string to repeat it 4 times,
"Hello".repeat // 'HelloHelloHelloHello'
Don’t Miss: Web Api Security C# Interview Questions
-For loop statement:-
A while statement executes its statement until a specified condition evaluates to true, a while statement looks like as follows:-
So this is called variable typing. This concept of JS is similar to Java.
What Is Minimum Timeout Throttling
and the output would be in
Script loadedMy script is initialized
If you don’t use setTimeout, the order of logs will be sequential.
and the output is,
David Goggins is an ultramarathon runner, a public speaker, a retired navy SEAL, and the author of the book ‘Can’t Hurt Me: Master Your Mind and Defy the Odds‘. He’s one of my role models because of his physical strength and mental resilience.
Well, if you’re ready, let’s explore this together.
What Do Mean By Prototype Design Pattern
The Prototype Pattern produces different objects, but instead of returning uninitialized objects, it produces objects that have values replicated from a template or sample object. Also known as the Properties pattern, the Prototype pattern is used to create prototypes.
The introduction of business objects with parameters that match the database’s default settings is a good example of where the Prototype pattern comes in handy. The default settings for a newly generated business object are stored in the prototype object.
Also Check: What Is An Online Interview
How Do You Group And Nest Console Output
The console.group can be used to group related log messages to be able to easily read the logs and use console.groupEndto close the group. Along with this, you can also nest groups which allows to output message in hierarchical manner.
For example, if youre logging a users details:
console.group console.log console.log // Nested Groupconsole.group console.log console.log console.log console.groupEnd
You can also use console.groupCollapsed instead of console.group if you want the groups to be collapsed by default.
You May Like: What To Expect In An Interview
How To Remove All Line Breaks From A String
The easiest approach is using regular expressions to detect and replace newlines in the string. In this case, we use replace function along with string to replace with, which in our case is an empty string.
In the above expression, g and m are for global and multiline flags.
What Is The Distinction Between Client
You May Like: What To Ask About Benefits In An Interview
When Can You Have An Error That Says Undefined Value On The Screen
Well, if it appears, it simply means either the variable the user is considering doesnt exist in the code. It is also possible that the property is not present. In addition to this, the error can also be due to the fact that the variable hasnt been assigned any value. Users need to make sure of these three steps before they proceed with anything. If this error is ignored, it can sometimes affect the entire code.
Difference Between Async/await And Generators Usage To Achieve The Same Functionality
- Generator functions are run by their generator yield by yield which means one output at a time, whereas Async-await functions are executed sequentially one after another.
- Async/await provides a certain use case for Generators easier to execute.
- The output result of the Generator function is always value: X, done: Boolean, but the return value of the Async function is always an assurance or throws an error.
Also Check: How Do You Interview When You Have A Job
And throughout this entire process, I started to see a pattern in the most frequently asked JS interview questions.
In this article, I have tried to list the concepts which will cover 80% of any good JS interview.
So, if you are prepping for your next JS interview this is the perfect cheatsheet for you to review and solidify your skills. Go through this and you’ll be ready to rock.