





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A list of 46 JavaScript interview questions and their answers. It covers topics such as data types, functions, timers, exceptions, arrays, objects, comments, and ES6 features. The document also explains the difference between let and var, attributes and properties, and event.preventDefault() and event.stopPropagation() methods. It defines terms such as hoisting, scope, closures, and promises. useful for students preparing for JavaScript interviews or exams, or for those who want to refresh their knowledge of JavaScript concepts.
Typology: Exercises
1 / 9
This page cannot be seen from the preview
Don't miss anything!
JavaScript Interview Questions
Following are the JavaScript Data types:
isNan function returns true if the argument is not a number otherwise it is false. 'Typeof' is an operator which is used to return a string description of the type of a variable.
A prompt box is a box which allows the user to enter input by providing a text box. Label and box will be provided to enter the text or number.
The JavaScript this keyword refers to the object it belongs to. This has different values depending on where it is used. In a method, this refers to the owner object and in a function, this refers to the global object.
The setTimeout(function, delay) function is used to start a timer that calls a particular function after the mentioned delay. The setInterval(function, delay) function is used to repeatedly execute the given function in the mentioned delay and only halts when cancelled.
== is called as equality operator which returns true when the two operands are having the same value. === is called as strict equality operator which returns true when the two operands are having the same value and same type.
It can be done in the following way: document.getElementById("myText").style.fontSize = "20"; or document.getElementById("myText").className = "anyclass";
The NULL value is used to represent no value or no object. It implies no object or null string, no valid boolean value, no number and no array object.
Undefined value means the
Try… Catch---finally is used to handle exceptions in the JavaScript Try{ Code } Catch(exp){ Code to throw an exception } Finally{ Code runs either it finishes successfully or after catch }
The push method is used to add or append one or more elements to the end of an Array. The pop method is used to remove the last element from an Array.
● getElementsByTagName(‘tagname’): Gets all the elements that have the given tag name. ● querySelector(): This function takes css style selector and returns the first selected element.
EncodeURl() is used to convert URL into their hex coding. And DecodeURI() is used to convert the encoded URL back to normal.
A named function declares a name as soon as it is defined. It can be defined using function keyword as : Function named() { console.log(“Named function”); }
JavaScript variable arguments represent the arguments that are passed to a function.
The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes. Global Variables − A global variable has global scope which means it is visible everywhere in your JavaScript code. Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
A callback is a plain JavaScript function passed to some method as an argument or option. It is a function that is to be executed after another function has finished executing, hence the name ‘ call back ‘. In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions.
Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope. It gives you access to an outer function’s
scope from an inner function. In JavaScript, closures are created every time a function is created. To use a closure, simply define a function inside another function and expose it.
The JavaScript date object can be used to get a year, month and day. You can display a timer on the webpage by the help of JavaScript date object.
Both let and var are used for variable and method declaration in JavaScript. However, the most important difference between the two JS keywords is that while the var keyword is function scoped, the let keyword is block scoped.
It is a function that has no name. These functions are declared dynamically at runtime using the function operator instead of the function declaration. The function operator is more flexible than a function declaration. It can be easily used in the place of an expression. For example: var display=function() { alert("Anonymous Function is invoked"); } display();
There are two types of comments in JavaScript.
The isNan() function returns true if the variable value is not a number.
JavaScript debugger keyword sets the breakpoint through the code itself. The debugger stops the execution of the program at the position it is applied. Now, we
Scope in JavaScript is the area where we have valid access to variables or functions. JavaScript has three types of Scopes. Global Scope , Function Scope , and Block Scope (ES6).
A prototype in simplest terms is a blueprint of an object. It is used as a fallback for properties and methods if it does exist in the current object. It's the way to share properties and functionality between objects.
Explain about project you have worked on using HTML
ES6 features: let, const, arrow function, map function, spread operator, rest operator, class, destructuring, property shorthand, Modules, Template String, Arrow function, default function arguments, Array Helper Functions like filter, forEach, map, some, reduce, find, every.
Es5 : Es5 is the fifth edition of the ECMAScript, introduced in 2009. Primitive data types that are string, boolean, number, null, and undefined are supported by Es5. In Es5, we could define the variables by var keyword only. Both function and return keywords are used to define a function in Es5. In Es5, a for loop is used to iterate over elements. Es6 : Es6 is the sixth edition of the ECMAScript, introduced in 2015. For supporting unique values, a new primitive data type 'symbol' wasintroduced in Es6. In Es6, in addition to var: let and const were introduced. An arrow function is a newly added feature in Es6 in which we don't require the function keyword in order to declare the function. Es6 introduced for..of loop in order to iterate over the values of the iterable objects.
It is a way to extract data from arrays and objects into a single variable. It is possible to extract smaller fragments from objects and arrays using this method. Basically, we
break our object and array elements into floating variables. For e.g: Run below code in your system. let color=['pink', 'blue']; let [c1, c2] = color; console.log (c1, c2);
startsWith, endsWith, includes, repeat
Explain class, objects, Inheritance, Methods here
Both let and const have block scope. let and const variable used before it is declared will give you error. Variables once declared with let and const keywod can't be redeclared with the same name again. let: variables declared with the let keyword are mutable, which means that their values can be changed. const: variables declared with the const keyword are block-scoped and immutable. When variables are declared with the const keyword, their value cannot be modified or reassigned. const doesn't define a constant value, but it defines constant reference to a value.
We use class keyword to create a class in Es6. Only functions and constructors are allowed in a class definition. Constructors in classes are responsible for initialising the properties of class's Objects. syntax: class className { }
If no value or undefined is passed, we can use the default parameters to set default values for named parameters. For e.g: Run below code in your system. const execute = (x , y = 2) => { console.log(x + " " + y); } execute(1);
Variable Hoisting does not apply to let bindings in ES6, so let declarations do not rise to the top of the current execution. Hence, let and const variable used before it is declared will give you error (ReferenceError). From the beginning of the block until the initialization is performed, the variable is in a "temporal dead zone". For e.g: Run below code in your system. console.log(b); let b = 2;
Unlike regular functions, arrow functions do not have their own this. Arguments objects are not available in arrow functions, but are available in regular functions. Regular function can easily construct objects. arrow function cannot be used as a constructor. You can return values from the arrow function the same way as from a