JavaScript Programming Technical Interview Questions

What is JavaScript?

JavaScript is a type of scripting language, also known as interpreted programming developed by Netscape. It is similar in nature and capability to Visual Basic, Tcl, Perl, and REXX. A benefit of scripting languages like JavaScript is that they are easier to code than compiled programming languages such as C and C++. However, scripting languages do tend to take longer to process and compile.

JavaScript is primarily used to create dynamic content in web browser applications, and can be used in nearly all browsers today. JavaScript is one of the three core technologies of World Wide Web content production along with HTML and CSS, Today, majority of the websites employ it and nearly all modern Web browsers support it without the need for plug-ins.

JavaScript is a prototype-based programming language that includes first-class functions. It is called a multi-paradigm language, as it supports three different programming styles: object-oriented, imperative, and functional. It also has an API that works with text, arrays, dates and regular expressions. However, it does not include any I/O functions, such as networking, storage, or graphics facilities.

It should be noted that JavaScript is in no way related to Java, which is a completely different programming language developed by Sun Microsystems. The only thing they have in common is the name, and the fact that they are both programming languages.  

What is the difference between JavaScript and Java?

The primary difference between Java and JavaScript is that Java is a programming language, whereas JavaScript is a scripting language. In fact, the two are not related as all. JavaScript is only named as use because the term Java was quite popular at the time.

Java

JavaScript

Java is an OOP programming language

Java Script is an OOP scripting language.

Developed by Sun Microsystems, not part of Oracle

Developed by Netscape

Creates applications that run in a virtual machine or browser

Run on a browser only

Create individual programs and applications

Makes dynamic webpages

Writes code for stand-alone applications.

Does not create applets or stand-alone applications.

Write independent code

Embedded into HTML code

Code needs to be compiled

Code is in text

Statically typed language

Dynamic language

Class-based

Prototype-based

Constructors are special functions that can only be called at object creation

Constructors are standard functions

Requires all non-block statements to end with a semicolon

Inserts semicolons at the ends of certain lines

Uses block-based scoping

Uses function-based scoping

Has an implicit this scope for non-static methods, and implicit class scope

Has implicit global scope

What is the real name of JavaScript?

JavaScript was originally developed under the name Mocha, and was officially called LiveScript when it first shipped. However, the name was soon changed to JavaScript as a marketing ploy to capitalize on the fame of Java, despite of the fact that it has nothing to do with Java.

Is JavaScript case sensitive language?

Yes, JavaScript is a case sensitive language, which means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. For example, the while keyword must be always typed as "while" throughout the code, and not as "While" or "WHILE".

How to write a hello world example of JavaScript?

JavaScript is an interpreted language, which means that there is no need for compilation. The code can be just written and attached to HTML code. Here is an example code for Hello World.

<!DOCTYPE HTML>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

Just write this code in a word editor, save the file as .html and load.

What is the difference between JavaScript and jscript?

JavaScript and jscript are two completely different components even though they may sound similar. However they are often confused due to the way jScript came around.  Sun Microsystems developed JavaScript in 1995. However, as it was proprietary language, it could not be used by its competitors such as Microsoft. So, Microsoft reverse engineered its own language from JavaScript. In order to avoid issues with the trade name, Microsoft called jscript. It was developed primarily to run in Internet Explorer.

JavaScript

jScript

Is a language

Is a library

Developed by Sun Microsystems

Developed by Microsoft

Is an ECMAScript language specification

Is an ECMAScript language specification

Is an independently developed language

Reverse engineered from JavaScript by Microsoft

Used for developing client applications and server applications

Used for creating active and also online content for the world wide web.

Does not support conditional compilation

Supports conditional compilation, which allows a programmer to selectively execute code within block comments.

Few versions of JavaScript have access to ActiveX objects

All versions of JScript have access to ActiveX objects

How to use an external JavaScript file?

External JavaScript files are files that are saved with a .js extension. These files can be embedded into an html page, hence allowing one to use JavaScript functions on an html page. The benefit of using an external JavaScript file is that it allows code reusability, i.e. one can use the same file, i.e. code in numerous html pages. It is recommended that all JavaScript files be embedded into a single file as this increases the speed of the webpage.

JavaScript files can be used to include both types of scripts, i.e. the ones placed in head (functions), and the ones places in the body, which are the ones that run when the page loads.

One significant condition in using external JavaScript files is that the file should not include the <script> tag. It should only include HTML commenting and JavaScript code.

What is BOM?

BOM stands for Browser Object Model. It consists of the objects: navigator, history, screen, location and document, all of which are children of window.

The primary function of BOM is to allow JavaScript to "talk to", i.e. communicate with the browser. Basically, it allows one to interact with the browser.

The default object of browser is window. This means that one can call all the functions of the window wither directly, or by specifying window.

For example:

window.alert("hello javatpoint"); 

would work the same as:

alert("hello javatpoint"); 
What is DOM? What is the use of document object?

DOM stand for Document Object Model. It primarily deals with the document, as well as the HTML elements themselves, such as document and all traversal that can be done in it, such as events, etc.

It can be said that DOM is a subset of BOM, as each HTML page which is loaded into a browser window becomes a Document object. Also, document object is an object in BOM.

DOM defines the logical structure of documents. It defines how document is accessed and manipulated. DOM can also be used to create and build documents, as well as navigate their structure, and add, modify, or delete elements and content. Nearly anything that can be found in the an HTML or XML document can be accessed, changed, deleted, or added by using DOM.

What is the use of window object?

Window is not the object of Javascript, but rather an object of the browser. The window object represents an open window in a browser.

If a document contains frames, i.e. the <iframe> tags, then the window object can be used to make the browser create one window object for the HTML document, and one additional window object for each frame.

It should be noted that there are no official standards for the Browser Object Model (BOM). However, most browsers do support it as most modern browsers have implemented the same methods and properties for JavaScript interactivity. Hence, they all support the methods and properties of BOM.

When the window object is used, all global JavaScript objects, functions, and variables automatically become members of the window object.

What is the use of history object?

The history object is a type of JavaScript object. It is a part of the window object and is accessed through the window.history property.

The history object can be used to access all the URLs that are visited by the user within the browser window. It can be written without the window prefix.

In order to protect the privacy of the users, there are limitations to how JavaScript can access this object. Some of the ways that JavaScript can access it is via the following methods:

  • history.length – returns the number of URLs in the history list of the current browser window.
  • history.back() – same as clicking back in the browser. It loads the previous URL in the history list.
  • history.forward() – same as clicking forward in the browser. It loads the next URL in the history list.
  • history.go() – same as entering a specific URL. It loads a specific URL from the history list.
How to write comment in JavaScript?

There are two types of comments in JavaScript that one can write.

In order to write a single line comment in JavaScript, one has to type two slashes "//" in front of the code or text. This line of text or comment will then be ignored by the JavaScript interpreter.

For example:

<script type="text/javascript">

<!--

// This is a single line JavaScript comment

In order to write a multi-line comment, one has to begin the comment with /* and end it with */. The text between /* and */ will be ignored by JavaScript. The benefit of this is that one can write a longer comment than what would fit on a single line, and not have to break it up in order to fit on a single line.

For example:

<script type="text/javascript">

<!--

/*this is a multi-line comments

which will be ignored

by JavaScript;*/

//-->

</script>

These comments are visible only in the code but not in browser. These comments can be useful to clarify or indicate something, such as changes to be made to either yourself or to somebody else. It can be used to communicate with other developers or to pass on instructions. Hence, comments can be quite useful.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.