JavaScript Interview Question Set 1
Categories: Java 8(JDK1.8)
How to write a comment in JavaScript?
There are two types of comments in JavaScript.
a) Single Line Comment: It is represented by // (double forward slash)
b) Multi-Line Comment: Slash represents it with asterisk symbol as /* write comment here */
How to create a function in JavaScript?
To create a function in JavaScript, follow the following syntax.
function function_name(){
//function body
}
What are the different data types present in JavaScript?
There are two types of data types in JavaScript:
a) Primitive data types
b) Non- Primitive data types
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.
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>";
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";
How to create objects in JavaScript?
There are 3 ways to create an object in JavaScript.
a) By object literal
b) By creating an instance of Object
c) By Object Constructor
Let's see a simple code to create an object using object literal.
emp={id:102,name:"Rahul Kumar",salary:50000}
How to create an array in JavaScript?
There are 3 ways to create an array in JavaScript.
a) By array literal
b) By creating an instance of Array
c) By using an Array constructor
Let's see a simple code to create an array using object literal.
var emp=["Shyam","Vimal","Ratan"];
What does the isNaN() function?
The isNan() function returns true if the variable value is not a number. For example:
function number(num) {
if (isNaN(num)) {
return "Not a Number";
}
return "Number";
}
console.log(number('1000F'));
// expected output: "Not a Number"
console.log(number('1000'));
// expected output: "Number"
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).
function display()
{
document.writeln(10+20+"30");
}
display();
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 +).
function display()
{
document.writeln("10"+20+30);
}
display();