Home / Interview / JavaScript :: General Questions

Interview :: JavaScript

21) How to create a function in JavaScript?

To create a function in JavaScript, follow the following syntax.

22) What are the JavaScript data types?

There are two types of data types in JavaScript:

  1. Primitive Data Types - The primitive data types are as follows:
  2. Data TypeDescription
    Stringrepresents a sequence of characters, e.g., "hello"
    Numberrepresents numeric values, e.g., 100
    Booleanrepresents boolean value either false or true
    Undefinedrepresents an undefined value
    Nullrepresents null, i.e., no value at all
  3. Non-primitive Data Types - The non-primitive data types are as follows:
  4. Data TypeDescription
    Objectrepresents an instance through which we can access members
    Arrayrepresents a group of similar values
    RegExprepresents regular expression
23) What is the difference between == and ===?

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

24) 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:

25) 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:

26) How to create objects in JavaScript?

There are 3 ways to create an object in JavaScript.

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

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

27) How to create an array in JavaScript?

There are 3 ways to create an array in JavaScript.

  1. By array literal
  2. By creating an instance of Array
  3. By using an Array constructor

Let's see a simple code to create an array using object literal.

28) What does the isNaN() function?

The isNan() function returns true if the variable value is not a number. For example:

29) What is the output of 10+20+"30" in JavaScript?

3030 because 10+20 will be 30. If there is numeric value before and after +, it treats as binary + (arithmetic operator).

30) 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 +).