JavaScript Interview Questions and Answers Set 5

41. How to write comment in JavaScript?

There are two types of comments in JavaScript.

  1. Single Line Comment: It is represented by // (double forward slash)
  2. Multi Line Comment: It is represented by slash with asterisk symbol as /* write comment here */

42. What is the difference between == and ===?

The == operator checks equality only whereas === checks equality and data type i.e. value must be of same type.

43. How to write html code dynamically using JavaScript?

The innerHTML property is used to write the HTML code using JavaScript dynamically. Let’s see a simple example:

document.getElementById(‘mylocation’).innerHTML=”<h2>This is heading using JavaScript</h2>”;   

44. How to write normal text code using JavaScript dynamically?

The innerText property is used to write the simple text using JavaScript dynamically. Let’s see a simple example:

document.getElementById(‘mylocation’).innerText=”This is text using JavaScript”;   

45. How to create objects in JavaScript?

There are 3 ways to create object in JavaScript.

  1. By object literal
  2. By creating instance of Object
  3. By Object Constructor

Let’s see a simple code to create object using object literal.

emp={id:102,name:”Rahul Kumar”,salary:50000}   

JAVASCRIPT TRAINING
Weekend / Weekday Batch

46. What does the isNaN() function?

The isNaN() function returns true if the variable value is not a number.

47.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. The Prototype pattern is hardly used in traditional languages, however, it is used in the development of new objects and templates in JavaScript, which is a prototypal language.

48. What is the output of “10”+20+30 in JavaScript?

102030 because after a string all the + will be treated as string concatenation operator (not binary +).

49. Difference between Client side JavaScript and Server side JavaScript?

Client side JavaScript comprises the basic language and predefined objects which are relevant to running java script in a browser. The client side JavaScript is embedded directly by in the HTML pages. This script is interpreted by the browser at run time.

Server side JavaScript also resembles like client side java script. It has relevant java script which is to run in a server. The server side JavaScript are deployed only after compilation.

50. What is the real name of JavaScript?

The original name was Mocha, a name chosen by Marc Andreessen, founder of Netscape. In September of 1995, the name was changed to LiveScript. In December 1995, after receiving a trademark license from Sun, the name JavaScript was adopted.