Javascript Interview Questions and Answers – Part11

101. What is inside the global object?
When the global object is created, it is given some initial properties. These properties include:
global functions such as parseFloat(), and eval()
global properties such as NaN and Inifinity
global objects such as Intl and Math

102. What is top-level Javascript code?
Top-level Javascript code is Javascript code that is not part of a function – in other words any Javascript code that is outside of a function is considered to be top level code.
You can actually reference the global object in top-level Javascript code. In order to do that, you would use the Javascript keyword this. Here is what it would look like:
//the code below uses an arbitrary variable name – globalObj –
//to reference the global object
var globalObj = this;

103. Is the Window object a global object also?
If you have ever dealt with client side Javascript – and by client side Javascript we just mean Javascript used by a web browser – then you have probably heard of the Window object. The Window object also acts as a global object for any and all of the Javascript code within a browser window. Remember that the Window object is basically a representation of an individual browser window.
The Window object can actually be used instead of the this object to reference the global object. This is because the Window object not only has the core global properties, but it also has many other globals that have to do with web browsers and client-side Javascript.

104. What is a wrapper object in Javascript?

Provide an example of how a wrapper object would work.
In order to best explain what wrapper objects are and how they work in Javascript, let’s first consider a simple example of a string in Javascript.
var strng = “ProgrammerInterview is Great!”;
//the letter variable will hold a “P”:
var letter = strng.charAt(0);
In the code above, the strng variable is set to a string – “ProgrammerInterview is Great!”. Then, the first letter of that string is retrieved and stored in the letter variable – this would be the letter “P”.
Strings are not objects in Javascript
You may or may not know that in Javascript, objects are composed of properties. Those properties are accessed by using the dot notation – the “.”. And, if there is a function defined inside an object, that function is called a method. A method is also considered to be a property of an object.

105. In Javascript, Strings are not actually objects. So, the question is in our example above, why does it appear that charAt is a method belonging to a string object named strng?
Well, what’s actually happening is that a wrapper object is being created. What is a wrapper object? Well, as soon as we make the call to charAt – in effect, what looks like a method access of a String object named strng – then Javascript will actually convert the string value to an object. You can think of it as Javascript making a call to new String(strng) behind the scenes.
The wrapper object will inherit all of the string methods, like charAt. And as soon as the property – in this case the charAt method – is correctly used, then the object that has just been created is thrown away. So, another word for the wrapper object is a “transient object”, because transient means something that appears just temporarily and then disappears.
Wrapper objects also apply to both numbers and booleans in Javascript, so that methods can be called on those types as well. Remember that the whole point of a wrapper object is to allow methods to be called on non-objects.
Finally, let’s consider one last example:
var someString = “hello”;
someString.size = 5;
// s will be undefined:
var s = someString.size;
In the code above, someString is obviously a string set to “hello”. We then create a temporary String object and set a property named “size” on the someString string. The object, however, is then thrown away. So, when we try to access the size property in the third line, the variable “s” will actually be undefined, because that property does not exist since the String object created was only temporary.
The whole point of the code above is to demonstrate the fact that when you try to set the value of a property on a string then it simply won’t work, because it’s just set on temporary object. But, when you try to read the value of a property (like we did in our earlier example using charAt), then it works just fine. This is because of the fact that the string will act like an object temporarily and allow you to use a method – well, it’s actually a wrapper object, but hopefully you understand the difference now!

106. What is ECMAScript?
When reading about Javascript, you may come across the term ECMAScript. What is ECMAScript? ECMAScript is a standard for a scripting language, and the Javascript language is based on the ECMAScript standard.

107. Is Javascript exactly the same as ECMAScript?
No, Javascript is not exactly equivalent to ECMAScript. The core features of Javascript are based on the ECMAScript standard, but Javascript also has other additional features that are not in the ECMA specifications/standard.

108. Do other languages use the ECMAScript standard?
Yes, there are languages other than Javascript that also implement the ECMAScript Standard as their core. ActionScript (used by Adobe Flash) and JScript (used by Microsoft) are both languages that implement the ECMAScript standard. The best way to think of languages that implement the ECMAScript standard (including Javascript) is as dialects that all share the same “core” structure, but each language adds their own style and “tone” on top of the ECMAScript standard.

109. What is the latest version of ECMAScript?
ECMAScript version 5 was finished in December 2009, the latest versions of all major browsers (Chrome, Safari, Firefox, and IE) have implemented version 5. Version 5.1 was finished in June, 2011.

110. Why is it called ECMAScript?
Javascript was originally created at Netscape, and they wanted to standardize the language. So, they submitted the language to the European Computer Manufacturer’s Association (ECMA) for standardization. But, there were trademark issues with the name Javascript, and the standard became called ECMAScript, which is the name it holds today as well. Also because of trademark issues, Microsoft’s version of the language is called JScript – even though JScript is, at it’s core, the same language as Javascript.

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 *

× 9 = sixty three

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