Javascript Interview Questions and Answers – Part9

81. Explain the for-in loop?
The for-in loop is used to loop through the properties of an object.
The syntax for the for-in loop is –

for (variable name in object){
statement or block to execute

In each repetition, one property from the object is associated to the variable name, and the loop is continued till all the properties of the object are depleted.

82. What is Javascript minification?
Minification in Javascript is the process of removing all characters that are not necessary from the Javascript source code. That is why it is called “minification” – because all of the data that is not necessary to the functioning of the Javascript is removed from the source code, and therefore the Javascript is “minimized”. Even though these characters are removed from the Javascript source code, the functionality of the Javascript code does not change at all. So, your Javascript code will behave exactly the same even after it goes through the minification process. Code that has gone through the minification process is also known as “minified” code.

83. What data is removed in Javascript minification?
The data that is removed in Javascript minification is extra whitespace, comments, new line characters, etc. Basically anything that can be removed without compromising the functionality of the Javascript is removed.

84. How are Javascript files minified?
There are a lot of programs that will minify your Javascript code for free – all you have to do is simply google “Javascript minifier”, and these sites will simply ask you to submit your Javascript code and will return the minified result.

85. What are the benefits and advantages of Javascript minification?
The main purpose of Javascript minification is to speed up the downloading or transfer of the Javascript code from the server hosting the website’s Javascript. The reason that minification makes downloads go faster is because it reduces the amount of data (in the minified Javascript file) that needs to be downloaded. Less data means that the user’s browser spends less time processing that data, which is why time is saved. So, we can say that minification is performed on Javascript source code because it is essentially a performance enhancement – and it allows websites that use minified Javascript to load faster.
To summarize, some of the benefits of Javascript minification are:
*  If you are a website owner, your users will have quicker download times for your webpages because there is now less data in your Javascript files that needs to be downloaded.
* Also, if you are a website owner, your site will have less bandwidth consumption. This means that your servers will consume less energy and you will save money. If your site just has a few visitors everyday, the bandwidth savings will be meaningless. But, think of a large site like Facebook, which gets hundreds of millions of users each day, and how Javascript minification could save a lot of bandwidth for them.
Minification is NOT like normal data compression
Normally when you think of data compression, one of the first things that may come to mind is “zipping” a file. When you zip a file, you are compressing it using a special algorithm, and when someone else has to unzip that file, they have to decompress the file using a special program (which you may have used without knowing it since it comes with most operating systems built in). However, with Javascript minification, no special program is needed by the browser in order to understand how to read the minified Javascript – because minified Javascript is still readable by the browser as is. A human can even read it – although with great difficulty since there won’t be any whitespace and newline characters. But, the point is that no intermediate program is needed in order to convert the minified Javascript into a form that is readable by the browser, and for that reason it is very different from normal data compression.
Minification does apply to other programming languages as well
Even though we discuss minification within the context of Javascript in this article, it does apply to other languages as well. For example, both CSS and HTML can be and often are minified for the same reasons as Javascript.
Obfuscation in Javascript
You may have also heard of the term obfuscation used in conjunction with the term minification in Javascript. You can read an easy to understand article about the difference between obfuscation and minification here: Minfication vs Obfuscation.

86. What is the difference between minification and obfuscation?
If you’re just becoming familiar with Javascript, then you have probably come across two terms – minification and obfuscation – and you may not be sure what exactly is the difference between the two terms.
The goal of minification is to improve performance
Javascript minification is a process in which Javascript files are modified (by a program that is called a “minifier”) by removing all unnecessary characters in the file. This makes the Javascript file much “lighter” (since it now contains less characters), which means that browsers can then download and process the file much faster. So, the goal with minification is to improve performance. You can also read more about minification in Javascript here:Javascript Minification.
The goal of obfuscation is to try to hide your original source code from other people
Obfuscation in Javascript is also a process in which Javascript files are modified. But, in obfuscation, the files are modified to deliberately make them difficult to understand and read. The goal is to make it so that people can not reverse engineer Javascript source code and use it for their own projects, or whatever else. But, if someone is determined enough they can and will reverse engineer any obfuscated Javascript code so that they can see the original source code – obfuscation will just slow them down a bit. Remember that browsers have to be able to translate the obfuscated Javascript source code to it’s original form so that they can understand it. And if browsers can do it, then any individual can also do it with some effort.
Obfuscation vs Minification
Hopefully now you see that although obfuscation and minification both modify the Javascript source code, they both try to accomplish two very different things. And, that is the key difference.

87. What is the meaning of “falsy” in Javascript?
As you probably already know, Javascript has the Boolean values true and false. But, what’s interesting and important is that you understand the fact that everything in Javascript has a built-in Boolean value in addition to it’s ‘obvious’ value, and that Boolean value is known as either falsy or truthy. This may sound confusing, but read on and the concept should become clearer.
Falsy values in Javascript
Here’s a complete list of falsy values in Javascript:
false (the boolean false is also considered falsy)
“” (an empty string)
0 (zero)
NaN (a property that represents the “Not-a-Number” value –
indicating that a value is not a legal number)
The list above is the complete list of falsy values in Javascript.
Truthy values in Javascript
Now you’re probably wondering about truthy values. Well, every value that’s not in the list above is a truthy value. Even if we take false and put it in quotes – like this: “false” – then that is still a truthy value.
Why falsy values can be dangerous
Suppose we have the following code in an if statement:
…//do something

The code above checks to see if the ‘findthis’ text is somewhere inside of someString, and if it is it returns the index, which is the position inside someString where the substring ‘findthis’ starts. The problem with this code is the fact that the someString string could very well start with ‘findthis’ (the ‘someString’ string could be something like ‘findthisxzyabc’), which would mean that the indexOf check would return a 0 (because 0 is the position for the very first character in the string). And because 0 is a ‘falsy’ value – meaning that it is treated as a boolean false inside the if statement – then the if statement would evaluate to false even though it should be true in this situation because of the fact that the substring ‘findthis’ was actually found inside someString.
So, you should be very careful when writing your if statements – especially when checking properties that have the potential to return a falsy value. In that situation you should be prepared to handle it appropriately.
Javascript falsy tests:
It’s worth taking some time to go over the different falsy values to test them out and see how they compare to each other.
False, 0, and “” – all falsy values – are considered to be equivalent and can be compared to one another. This is what that would look like:
0 == false //this is true
“” == false //this is true
0 == “” //this is also true
However, the values undefined and null are considered to not be equivalent to anything except themselves. So, here is what we would have:
(null == false) // false
(null == null) // true
(undefined == undefined) // true
(undefined == null)// true
And the strange thing about NaN is that it is not considered to be equivalent to anything and that includes itself:
NaN == undefined //false
NaN == NaN //false

88. What is method chaining? 
Well, from a high level, think of it as a chain of method calls. More specifically, if you have some methods that return objects when invoked, then what you can do is use an object returned from one method invocation to invoke yet another method. This process of can then be continued indefinitely – where one method call is used to make another method call – forming a “chain” of method calls.
This chain is just a single expression. It’s easy enough to break this chain down and make each of the calls into separate expressions, but then it would obviously not be method chaining anymore.
Example of Method Chaining Pattern in Javascript
Actually, the JQuery library makes extensive use of method chaining. Let’s go through an example so that it’s clear to you how method chaining works. Here is an example of what method chaining in would look like using JQuery:
$(“#h1”).text(“Change text”).css(“color”, “red”);;
In the example above, the h1 element will have it’s text changed to “Change text”, and then the color of the text will also be changed to red.
Method Chaining Return Value
If you create a Javascript method that does not have a return value, then you might want to have that method return this. If this is returned from a method then you can perform method chaining on the methods that you define. Remember that method chaining will only work if the return value of the methods being chained is an actual object.

89. Does Javascript support nested functions? How do they work?
In Javascript, it is possible to nest functions – so that functions may be defined inside other functions.
Javascript Nested Functions Example
Here’s an example of what nested functions in Javascript would look like:

function circumference (radius)
// nested function:
function diameter(rad) { return 2*rad; }

// call the nested function
return Math.PI * diameter(radius);

The function circumference calculates the circumference of a circle when given the radius as an input. You should pay special attention to the call to the inner function diameter, which will return the diameter of a circle by simply multiplying the radius by 2.
Javascript Nested Functions Scope
Nested functions have interesting scoping rules. A nested function can access any variables and parameters of the outer function(s). In our example above, you can see that the nested function – diameter – is passed the radius parameter which is a parameter defined by the outer function named circumference. This is of course because any nested function has access to the parameters of it’s outer function.

90. What is the global object in Javascript? How does it work?
The global object is very important in Javascript. It contains certain properties that are widely used – remember that in Javascript, the members of an object are typically referred to as “properties”. These properties include certain functions and even other objects, and are all available to any given Javascript program.

Sign-up for our email newsletter and get free job alerts, current affairs and GK updates regularly.
Subscribe Here (or)

Leave a Reply

Your email address will not be published. Required fields are marked *

five + = 7

Popup Dialog Box Powered By :
  • RSS
  • Facebook
  • Google+
  • Twitter