Iteanz Interview Questions | Latest Technologies Interview Questions

Javascript Interview Questions

Written by Bharathkumar | Sep 9, 2022 5:24:21 AM

Q1. What is JavaScript?


Ans: JavaScript is a high-level, dynamic, untyped, and interpreted programming language. It has been standardized in the ECMAScript language specification.Alongside HTML and CSS, it is one of the three core technologies of World Wide Webcontent production; the majority of websites employ it and it is supported by all modern Web browsers without plug-ins. JavaScript is prototype-based with first-class functions, making it a multi-paradigm language, supporting object-oriented, imperative, andfunctional programming styles. It has an API for working with text, arrays, dates and regular expressions, but does not include any I/O, such as networking, storage, or graphics facilities, relying for these upon the host environment in which it is embedded”.

Q2. What is an ECMAScript?

Ans: ECMAScript (European Computer Manufacturers Association) Script is a specification for the scripting language standards. It has standardized Javascript which made Javascript the best implementation of ECMAScript.

Q3. What are the new ways to define a variable in Javascript?

Ans: There are three possible ways of defining a variable in Javascript (i) var (which is used from the beginning) (ii) const (iii) let. The last two ways are the latest ways of defining a variable and are introduced in ES-2015(ES6 version).

Q4. What is the difference between Local Storage and Session Storage?

Ans: Local Storage will stay until it is manually cleared through settings or program.

Session Storage will leave when the browser is closed.

List  some of the Javascript frameworks.

There are many Javascript Frameworks available today, but the most commonly used frameworks are:

  1. Angular
  2. React
  3. Vue

Q5. What is the difference between the keywords var and let?

Ans: The keyword var is from the beginning of Javascript; whereas, let is introduced in ES2015/ES6. The keyword let has a block scope; whereas, the keyword var has a functional scope.

Q6. What Are The Different Objects Used In JavaScript?

Ans: JavaScript uses a hierarchical structure, applicable to all the objects created in a document. Following are the objects, used in JavaScript that shows the relationship of one object to another.

  1. Window Object: It is the topmost object in the hierarchy. It refers to the content area of the browser window that consists of HTML documents. Each frame is also a window that has some actions inside it.
  2. Document Object: A Document object represents the HTML document that the window will display. It has various properties that refer to other objects, which allow access to and modification of content in the document.
  3. Form Object: A form object is used to take user data as input for processing. It corresponds to an HTML input form constructed with the <FORM>…</FORM> tag.

Q6. What Are JavaScript Data Types?

Ans: JavaScript supports three Primary, two Composite and two Special data types. Next, we list down the data types in each of the categories.

Primary Data Types:

  • String
  • Number
  • Boolean

Composite Data Types:

  • Object
  • Array

Special Data Types:

  • Null
  • Undefined

Q7. What Is <This> In JavaScript?

Ans: All the prime languages use ‘this’ keyword to refer to an object that is currently instantiated by the class. However, in JavaScript, ‘this’ refers to an object which ‘owns’ the method. Though this varies, with how a function call happens.

Q8. What is the difference between the operators '=='  and '==='?

Ans: The operator '==' compares the value; whereas, the operator '===' compares both value and type.

Q9: What is the difference between null and undefined?

Ans: When used the typeof operator on null; i.e., typeof(null), the value is an object. Whereas, when used the typeof operator on undefined; i.e., typeof(undefined), the value would be undefined.

Q10: What is the difference between let and var?

Ans: There are several differences between let and var. let gives you the privilege to declare variables that are limited in scope to the block; statement of expression, unlike varvar is rather a keyword, which defines a variable globally regardless of block scope.

  • Global window object: Even if the let variable is defined as same as var variable globally, the let variable will not be added to the global window object. The similarities are alike when both are used outside the function block.
  • Block: let variables are usually used when there is a limited use of those variables. Say, in for loops, while loops or inside the scope of if conditions etc. Basically, where ever the scope of the variable has to be limited.
  • Redeclaration: let variables cannot be re-declared while var variable can be re-declared in the same scope.
    Function: let and var variables work the same way when used in a function block

Q11: What Are The Different Ways To Create An Array In JavaScript?

Ans: There are two main ways to create an array in JavaScript

  1. Using An Array Initializer (Array Literal).
    The array initializer (array literal) syntax is simple. It is a comma-separated list of values in square brackets.
    Examples:
    var myArray1 = [1,2,3,4,5]      // an array with 5 elementsvar myArray2 = [5]              // an array with 1 element
    var myArray3 = [true,'Hi',[7]]  // element types need not be the same.
  2. Using The Array Constructor.

The Array constructor method has three different syntaxes. If we call the constructor with two or more arguments, it declares an array with array elements also initialized. If we provide only one argument to the Array constructor, it refers to the length of the new array with, elements not initialized. Lastly, the constructor without any argument creates an array with its length set to zero with elements not initialized.

Examples:

var myArray4 = new Array(1,2,3,4,5)  // an array with 5 elements

var myArray5 = new Array(20)        // an empty array of length 20

var myArray6 = new Array()           // an empty array of length 0

Q12. What Are JavaScript Cookies?

Ans: A cookie is a piece of data which is sent from a website (that owns the requested web page) and gets stored locally by the browser at the user end. Cookies are needed because HTTP protocol which arranges for the transfer of web pages to your browser, is stateless. It means that HTTP has no way to keep track of the activities performed by the user at an earlier point in time. One way to resolve this issue is by using cookies. It contains the following data.

  • A name-value pair containing the actual data.
  • An expiry date after which the cookie is no longer valid.
  • The domain and path of the server it should be sent to.

When a request arrives at the server for a web page that maintains a cookie, the server appends the cookie to the HTTP header to send it across. The server-side programs can then read out the information included in it and decide that you have the right to view the page or not and other user preferences.

Thus, every time you visit the site that maintains the cookies, your information is available there.

Q13. What Is The Naming Conventions For The Variables In JavaScript?

Below rules to be followed while naming the variables in JavaScript:

  • Do not use any of the JavaScript reserved keywords as a name for your variable. For example, the “break” or “boolean” is the JavaScript keywords, and if used as variable names, it’s invalid.
  • JavaScript variable names should not start with a numeral (0-9). It must begin with a letter or the underscore character. For example, 123varis an invalid variable name, but _123var is a valid one.
  • Also, JavaScript variable names are case sensitive. For example, test and Test are two different variables.

Q14. What Is The Strict Mode In JavaScript?

Ans: Strict Mode imposes a layer of constraint on JavaScript. It provides following enhancements.

  • JavaScript will throw an error if we try to use the elements of a deprecated language.
  • To use a variable, it has become mandatory to declare it.
  • It disallows duplicate property and parameter names.
  • The eval()method is safer to use, but still considered evil in some cases.
  • It deprecates the “with” statement.
  • JavaScript will throw an error if we try to assign a value to a read-only property.
  • It decreases the global namespace pollution.

To enable strict mode, we have to add, “use strict” directive to the code. The physical location of the “strict” directive determines its scope. If used at the beginning of the js file, its scope is global. However, if we declare strict mode at the first line in the function block, its scope restricts to that function only.

Q15. What is JavaScript Event Delegation Model?

Ans: In JavaScript, there is some cool stuff that makes it the best of all. One of them is the Delegation Model. When capturing and bubbling, allow functions to implement one single handler to many elements at one particular time then that is called event delegation. Event delegation allows you to add event listeners to one parent instead of specified nodes. That particular listener analyzes bubbled events to find a match on the child elements.

Q16. What are Closures in JavaScript?

Ans: Closures are the combination of lexical environment and function within which the function was declared. This allows JavaScript programmers to write better, more creative, concise and expressive codes. The closure will consist of all the local variables that were in scope when the closure was created. Sure, closures appear to be complex and beyond the scope, but after you read this article, closures will be much more easy to understand and more simple for your every day JavaScript programming tasks. JavaScript is a very function-oriented language it gives the user freedom to use functions as the wish of the programmer.

Q17. What Is The Prototype Property In JavaScript?

Ans: Every JavaScript function has a prototype property (by default this property ise null), that is mainly used for implementing inheritance. We add methods and properties to a function’s prototype so that it becomes available to instances of that function. Let’s take an example that calculates the perimeter of a rectangle.

function Rectangle(x, y) {

this.x = x;

this.y = y;

}

Rectangle.prototype.perimeter = function() {

return 2 * (this.x + this.y);

}

var rect = new Rectangle(4, 2);

console.log(rect.perimeter()); // outputs '12'

Q18. What close() does in Javascript?

Ans: In Javascript close() method is used to close the current window. You must write window.close() to ensure that this command is associated with a window object and not some other JavaScript object.

Q19. How to import all exports of a file as an object?

Ans: import * as object name from ‘./file.js’ is used to import all exported members as an object. You can simply access the exported variables or methods using dot (.) operator of the object.

Example:

objectname.member1;

objectname.member2;

objectname.memberfunc();

Q20. What are exports and imports?

Ans: Imports and exports helps us to write modular javascript code. Using Imports and exports we can split our code in to multiple files. Imports allows to take only some specific variables or methods of a file. We can import methods or variables that are exported by a module.See the below example for more detail.

//index.js

import name,age from './person';

console.log(name);

console.log(age);

//person.js

let name ='Sharad',

occupation='developer'

age =26;

export { name, age};

Related Interview Questions...

Core Java Interview Questions And Answers

Core Java Programming Interview Questions and Answers

Multithreading in Java Interview Questions and Answers

Java Interview Questions and Answers

Java/J2EE Apps Integration Questions and Answers.

Java Collections Interview Question and Answers

Interview Questions For Selenium with Java

Java Web Services Interview Questions and Answers

Tricky Java Interview Questions and Answers