JavaScript Interview Questions for Beginners

JavaScript is the popular programming language of the web. The high-level, interpreted programming language follows a multi-paradigm approach.

This article provides you with a comprehensive list of common JavaScript interview questions that often come up in interviews with great answers to them. It will also help you understand the fundamental concepts of JavaScript.

1. What do you mean by term JavaScript?

JavaScript is one of the popular web scripting language. It is different from Java language. JavaScript is used for client-side and server-side development. The JavaScript code can be inserted into HTML pages that can be understood and executed by web browsers while also supporting object-oriented programming abilities.

2. What are the features of JavaScript? List some of them

Some of the features of JavaScript are:

  • Lightweight programming language
  • Used especially for the development of the applications which are network-based
  • Object-oriented
  • Complementary to HTML
  • Open source
  • Interpreted programming language
  • Cross-platform compatible

3. What’s the difference between JavaScript and Java?

JavaScript is an object-oriented scripting language but Java is an object-oriented programming language.

Java applications are generally made for use in operating systems and virtual machines. JavaScript applications are meant to run a web application i.e inside a web browser.

JavaScript does not need compilation before running the application code.

Java source code needs a compiler before it can be ready to run in realtime.

4. What are the various data types in JavaScript?

These are the different types of data that JavaScript supports:

  • Boolean – For true and false values
  • Undefined – For variables that are only declared and not defined or initialized
  • Number – For integer and floating-point numbers
  • String – For characters and alphanumeric values
  • Object – For collections or complex values
  • Symbols – For unique identifiers for objects
  • Null – For empty or unknown values

5. How do you create an object in JavaScript?

Since JavaScript is essentially an object-oriented scripting language, it supports and encourages the usage of objects while developing web applications.

const student = {

    name: 'John',

    age: 17

}

6. How do you create an array in JavaScript?

Here is a very simple way of creating arrays in JavaScript using the array literal:

var a = [];

var b = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];

7. What are the escape characters in JavaScript?

In JavaScript, we use escape characters, typically backslash (\ \) while working with special characters, such as ampersands (&), apostrophes (‘), double quotes (“ “), and single quotes (‘ ‘). Whatever is enclosed within the escape, characters get displayed by JavaScript.

Six additional escape characters are also available in JavaScript:

  • \b – Backspace
  • \f – Form feed
  • \n – Newline
  • \r – Carriage return
  • \t – Horizontal tabulator
  • \v – Vertical tabulator

These aren’t in anyway executed in the HTML or JS code. These were originally designed for controlling fax machines, teletypes, and typewriters.

8. Define a named function in JavaScript.

The function which has named at the time of definition is called a named function. For example

function msg()  
{  
  	document.writeln("Named Function");  
}  
msg();

9. What is the ‘this’ keyword in JavaScript?

The ‘this’ keyword in JavaScript refers to the currently calling object. It is commonly used in constructors to assign values to object properties.

10. How would you create, read and delete a cookie in JavaScript?

The simplest way of creating a cookie using JavaScript is as below:

document.cookie = “key1 = value1; key2 = value2; expires = date”;

The document.cookie string keeps a list of name-value pairs separated by semicolons, where ‘name’ is the name of the cookie, and ‘value’ is its value. We can also use the split() method to break the cookie value into keys and values.

To delete a cookie, we can just set an expiration date and time. Specifying the correct path of the cookie that we want to delete is a good practice since some browsers won’t allow the deletion of cookies unless there is a clear path that tells which cookie to delete from the user’s machine.

function delete_cookie(name) {

  	document.cookie = name + "=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";

}