JavaScript Interview Questions - Set 1

JavaScript Interview Questions

Next →

This page consists of JavaScript interview questions and answers.

Q1: What is JavaScript?

JavaScript is a light-weight, browser-based and platform independent scripting language.

Q2: Can we use JavaScript on the server?

Yes. NodeJS is one example.

Q3: What is JavaScript and JScript?

JavaScript was provided by NetScape and Microsoft developed their own JavaScript and named it JScript to avoid copyright issues.

They are similar but with different names.

Q4: Write a simple JavaScript code to print "Hello World" in the browser console.

console.log("Hello World");

Q5: How will you write JavaScript code inside a HTML file?

For this we use the script tag and set the type attribute to text/javascript.

The following code can be added in a HTML file.

<script type="text/javascript">
console.log("Hello World");

Note! In newer browsers we can omit the type attribute.

Q6: How will you include an external JavaScript file in a HTML file?

For this we use the script tags and set the src attribute to the JavaScript file path that we want to include.

In the following example we are including a JavaScript file from the js directory.

<script src="js/script.js"></script>

Q7: What is the file extension used for JavaScript files?

We save JavaScript file using the .js extension.

Q8: How will you write "Hello World" in a paragraph in an HTML page using JavaScript?

For this we can use document.write() and pass the required string.

document.write('<p>Hello World</p>');

Q9: Print current date and time in the browser console using JavaScript

For this we create an instance of the Date.

var d = new Date();

The above code will give us output in the following format.

Wed Jan 01 2014 06:59:08 GMT+0530 (IST)

Q10: Write a JavaScript code to show an alert having message "Hello World"

For this we can use JavaScript alert() and pass the string Hello World.

alert('Hello World');
Next →