JavaScript Interview Questions (Part2)

What is JavaScript?

JavaScript is a scripting or programming language designed primarily for adding interactivity to Web pages and creating Web applications. It is an interpreted programming language with object-oriented capabilities. Alongside HTML and CSS, JavaScript is one of the three core technologies of the World Wide Web. It is supported by most web browsers including Chrome, Firefox, Safari, internet Explorer, Edge, Opera, etc. Most mobile browsers for smart phones support JavaScript too.

Which company developed JavaScript?

JavaScript was initially known by the name LiveScript. It was developed in mid nineties by a company called Netscape. The first JavaScript engine was created by Brendan Eich at Netscape, for the Netscape Navigator Web browser.

What data types are supported in Javascript?

According to the latest ECMAScript release, following are the data types supported in Javascript:

  1. Boolean
  2. Null
  3. Undefined
  4. Number
  5. String
  6. Symbol
  7. Object

What is the difference between JavaScript and Jscript?

Both JavaScript and jscript designed for to make web page More dynamic. JavaScript is a scripting language developed by Netscape Communications designed for developing client and server Internet applications. JScript is the Microsoft dialect of the ECMAScript scripting language specification. Microsoft reverse engineered Javascript and called it JScript to avoid trademark issues with Sun. JavaScript is the common term for a combination of the ECMAScript programming language plus some means for accessing a web browser's windows and the Document Object Model (DOM). It is used to develop client and server applications. It basically focuses how the page should react after triggering the JavaScript code.

What are the primitive data types in JavaScript?

Javascript has five primitive data types:

  1. Number
  2. String
  3. Boolean
  4. Undefined
  5. Null

What are the Non-primitive data types in JavaScript?

  1. Object
  2. Date
  3. Array

What is the scope of variables in JavaScript?

The scope of a variable is controlled by the location of the variable declaration, and defines the part of the program where a particular variable is accessible. JavaScript has two scopes: global and local. Any variable declared outside of a function belongs to the global scope, and is therefore accessible from anywhere in your code. A variable that is declared inside a function definition is local. It is created and destroyed every time the function is executed, and it cannot be accessed by any code outside the function.

Is JavaScript case sensitive? Give an example?

Yes, JavaScript is a case-sensitive language.

example

"This" is not same as "this"

What are all the types of Pop up boxes available in JavaScript?

  1. Alert Box
  2. Confirm Box
  3. Prompt Box

How to use external JavaScript file?

External JavaScript files have the file extension .js. External scripts are practical when the same code is used in many different web pages. This is the way to include an external javascript file to you HTML markup.

<script type="text/javascript" src="myscript.js"></script>

Where "myscript.js" is your external JavaScript file.

Can javascript code be broken in different lines?

You can break javascript code in different lines by using a backslash "\" at the end.

alert ( "Good morning halo world!!");

Note that this backslash approach is not necessarily preferred, and possibly not universally supported.

Which keyword is used to print the text in the screen?

Using document.write(), you can print the text in the screen.

Which symbol is used for comments in Javascript?

There are two types of comments symbol used in JavaScript.

Single Line Comments // and Multi-line comments start with /* and end with */.

example
//this is single line comments
/*This is an example of Multiline comment*/

Can I declare a variable as CONSTANT in JavaScript?

Yes. ECMAScript 2015 introduced the const keyword, but it thus far only enjoys a smattering of browser .

const CONSTANT_VAR = "some-value";

What is variable typing?

JavaScript is a very loosely typed language; the type of a variable is determined only when a value is assigned and can change as the variable appears in different contexts. This means that JavaScript variables can be reassigned to value of any type, so you don’t ever need to explicitly denote the type of variable or the return type of the function. JavaScript automatically adjusts the datatype while assigning or reassigning the variable.
x=100 x='1000'

What is === operator?

The "===" operator is a binary logical operator to check whether the two values are same and are of same data type. It will return false even when their values are equal but they are not of same data type.

For ex: 555 and '555', according to the values are same but they are not of same data type, hence === will return false.

example
var1=555 var2='555' b = (var1===var2);

The above code return false

What are the basic groups of dataypes in JavaScript?

Primary Data Types

  1. String
  2. Number
  3. Boolean

Composite Data Types

  1. Object
  2. Array

Special Data Types

  1. Null
  2. Undefined

What is the difference between null and undefined in JavaScript?

In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as:

var undefinedThis; alert(undefinedThis); //shows undefined alert(typeof undefinedThis); //shows undefined

null is an assignment value. It can be assigned to a variable as a representation of no value:

var nullValue = null; alert(nullValue); //shows null alert(typeof nullValue); //shows object

Between JavaScript and an ASP script, which is faster?

JavaScript is always faster. Java scripting is written for client-side , it does not need the web server's support for execution. It can be executed immediately or when some event occurs. ASP is a server side language so it does needs the assistance of the web server which is a time consuming process. Thus it will take time to send the request and get the response back from the server.
Javascript Interview questions and answers

How to disable an HTML object ?

document.getElementById("ctrl").disabled = true;

A disabled element is unusable and un-clickable. Disabled elements are usually rendered in grey by default in browsers to enable

document.getElementById("ctrl").disabled = false;

How do you access an element in an HTML document with JavaScript?

If you want to access any element in an HTML page, you always start with accessing the document object.

Finds the element with id="foo":

var myElement = document.getElementById("foo");

The getElementById() method returns the element that has the ID attribute with the specified value.

What is the use of window object?

The window object in JavaScript represent the current window open in the browser. It contains information related to the current browser window. All global JavaScript objects, functions, and variables automatically become members of the window object.

What does the "Access is Denied" IE error mean?

The reason is that Windows blocks one window or frame accessing another window or frame that is in a different domain.

How to detect browser name using JavaScript?

<script> alert('Your browser is : ' + navigator.appName + " " + navigator.appCodeName); </script>

How to get height and width of different browser in Javascript?

<script> var myWindow = window, doc = document, elem = doc.documentElement, body = doc.getElementsByTagName('body')[0], width = myWindow.innerWidth elem.clientWidth body.clientWidth, height = myWindow.innerHeight elem.clientHeight body.clientHeight; alert(width + " X " + height); </script>

What is screen object in JavaScript?

The window object also has a screen object with properties describing the physical display. It can be used to display screen width, height, colorDepth, pixelDepth etc.

<script> alert("Screen Width: "+screen.width + " Screen Height " + screen.height); </script>

How do you submit a form using JavaScript?

JavaScript provides the form object that contains the submit() method. You can perform javascript form submission by form attributes like id, name, class, tag name as well.

document.getElementById("thisForm").submit();

What are the decodeURI() and encodeURI()?

The encodeURI() function is used to encode a URI. It assumes that the input is a complete URI that might have some characters which need encoding in it. If you pass the result to decodeURI , the original string is returned.

Difference between window.onload and onDocumentReady?

The onDocumentReady allows you to execute code when only the DOM tree has been built, without waiting for images to load, while window.onload will wait until all assets have finished downloading, such as images and scripts.

What are JavaScript Cookies?

JavaScript cookies are the small test files stored in your system and it gets created when the user visits the websites to store information that they need. For ex. User Name details and shopping cart information from the previous visits.

Can you access Cookie using javascript?

Cookies are data, stored in small text files, on your computer. JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookie or cookies that apply to the current web page.

Whether JavaScript has concept level scope?

No. JavaScript does not have concept level scope. The variable declared inside the function has scope inside the function.

How to get the primitive value of a string in Javascript?

The valueOf() method is used to get the primitive value of a string Object. This value is equivalent to ring.prototype.toString().

How can you convert the string of any base to integer in JavaScript?

The JavaScript parseInt() Function, which convert the string of any base into integer in javascript.

example
ParseInt('string',Base(optional))

Base(optional): An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string.

example
var x = parseInt("1000", 10);