JavaScript Interview Questions And Answer
The JavaScript developer often face the Interview Questions when they meet viva board . Before facing viva board the javascript developer should prepare of questions that they are often faced in the interview . In here we have given some questions which is faced by the javascript developer many times . We are encourage JavaScript developer to prepare this questions before facing JavaScript Interview .
1. What is JavaScript ?
Ans: JavaScript is a scripting language and It is an object-based, lightweight, cross-platform translated language. It is widely used for client-side validation and JavaScript Translator (embedded in the browser) is responsible for translating the JavaScript code for the web browser.
2. Write some features of JavaScript ?
Ans:
- Complementary to HTML
- Complementary to Java
- Interpreted programming language
- Good for the applications which are network-centric
- Open source
- Cross-platform
3. Write some of the advantages of JavaScript ?
Ans:
- Interactivity is high
- Less Server Interaction
- Interfaces are richer
- Feedback to the visitors is immediate
4. What are JavaScript Data Types ?
Ans:
- Number
- String
- Boolean
- Object
- Null
5. Which is fasterBetween JavaScript and an ASP script ?
Ans:JavaScript is faster because JavaScript is a client-side language and thus it does not need the assistance of the web server to execute but ASP is a server-side language and hence is always slower than JavaScript.
6. How to write new line in javascript ?
Ans:Line Breaking within a string statement can be done by the use of a (backslash) '\', at the end of the first line
7. What will happen if an infinite while loop is run in Javascript ?
Ans: It will crash the browser.
8. What is prototypal Inheritance ?
Ans:Every object has a property called that is a prototype, where we can add methods to it and when you create another object from these the newly created object will automatically inherit its parent’s property.
9. Difference between “undefine” and “NULL” Keywords ?
Ans: When we define a var but not assign any value. typeof(undefine)=> undefine Null- manually done. typeof(null)=> object
10. Difference between “var” and “let”,"Const" Keywords ?
Ans:
Var is a variable which can be used over the program.
Let is an instant variable that can be used after a particular task.
Const is a constant variable which you can not change after initialize.
11. Explain function hoisting in JavaScript ?
Ans: JavaScript’s default behavior that allows moving declarations to the top is called Hoisting. There are 2 ways of
creating functions in JavaScript are Function Declaration and Function Expression. Let’s find out more about
these:
Function Declaration: A function with the specific parameters is known as function declarations. To create a variable in JavaScript is
called declarations.See the following example:
hoisted(); // logs "foo" function hoisted() { console.log('foo'); }Function Expression: When a function is created by using an expression its called function expression.
notHoisted(); // TypeError: notHoisted is not a function var notHoisted = function() { console.log('bar'); };
12. What are exports and imports ?
Ans: Imports and exports help us to write modular javascript code. Using Imports and exports we can split our code
into multiple files. Imports allow taking only few specific variables or methods of a file. We can import
methods or a 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};
13. What is difference between module.exports and export ?
Ans: The module is a plain JavaScript's object with an exports property. Exports is a plain JavaScriptvariable that
happens to set to module.exports. At the end of your file, node.js will ‘return’ module.exports to the
require function. The simplified way to view a JS file in Node could be this:
var module = { exports: {} }; var exports = module.exports; // your code return module.exports;If we set a property on exports, like exports.a = 9;, that will set module.exports as well because objects are passed around as references in JavaScript, which means that if we set multiple variables to the same object, they are all the same object; so then exports and module.exports are the same objects but if you set exports to something new, it will no longer be set to module.exports, so exports and module.exports are no longer the same objects.
14. How to import all exports of a file as an object.
Ans: import * as object name from ‘./file.js’ is used to import all the exported members as an object. You can simply access the exported variables or a methods using dot (.) operator of the object. See the following example:
objectname.member1; objectname.member2; objectname.memberfunc();
15. What is Javascript BOM ?
Ans: The BOM stands for “Browser Object Modal” that allows Javascript to ‘talk’ to the browser, no standards, modern browsers implement similar BOMS – window, screen, location, history, navigator, timing, cookies
16. What is the difference between the substr() and substring() functions in JavaScript ?
Ans:The substr() function has the form substr(startIndex,length) and It returns the substring from startIndex and returns ‘length’ number of characters.
var s = "hello"; ( s.substr(1,4) == "ello" ) // trueThe substring() function has the form substring(startIndex,endIndex) and It returns the substring from startIndex up to endIndex – 1.
var s = "hello"; ( s.substring(1,4) == "ell" ) // true
17. List HTML DOM mouse events ?
Ans:
- onclick()
- ondblclick()
- mousemove()
- mousedown()
- mouseover()
- mouseout()
- mouseup()
18. How to convert Javascript date to ISO standard ?
Ans: toISOString() method is used to convert javascript date to ISO standard and It converts JavaScript Date object into a string, using the ISO standard.
var date = new Date(); var n = date.toISOString(); console.log(n); // YYYY-MM-DDTHH:mm:ss.sssZ
19. How to get inner Html of an element in JavaScript ?
Ans: InnerHTML property of HTML DOM is used to get inner Html of an element in the JavaScript.
var inner= document.getElementById("inner").innerHTML ; console.log(inner); // This is inner Element document.getElementById("inner").innerHTML = "Html changed!"; var inner= document.getElementById("inner").innerHTML ; console.log(inner); // Html changed!
20. How to clone an object in Javascript ?
Ans: Object.assign() method is used for cloning a particular object in Javascript.Here is sample usage
var x = {myProp: "value"}; var y = Object.assign({}, x);
21. What are the primitive data types in JavaScript ?
Ans: The primitive is a basic data type that’s not built out of other data types and It can only represent one single value. All the
primitives are built-in data types by necessity, (the compiler has to know about them,) but not all built-in data
types are primitives.
In JavaScript there are 5 primitive data types are available they are undefined, null, boolean, string and
number are available and Everything else in Javascript is an object.
22. How to get the primitive value of a string in Javascript ?
Ans: In Javascript valueOf() method is used to get the primitive value of a particular string.
var myVar= "Hi!" console.log(myVar.valueOf())
23. Explain JavaScript Event Delegation Model ?
Ans: In JavaScript, there are some cool stuff that makes it the best of all and of them is Delegation Model. When capturing and bubbling, allow functions to implement one single handler to many elements at one particular time then that is called an event delegation. Event delegation allows you to add event listeners to one parent instead of a specified nodes and that particular listener analyzes bubbled events to find a match on the child elements but many people think it to be complicated but in reality, it is very simple if one starts understanding it.
4.Explain Arrow functions ?
Ans: An arrow function is a consise and short way to write function expressions in Es6 or above and arrow functions cannot be used as constructors and also does not supports this, arguments, super, or a new target keywords. It is best suited for a non-method functions. In general an arrow function looks like
const function_name=()=>{ //codes } const greet=()=>{ console.log('hello'); } greet();
25. What close() does in Javascript ?
Ans: In Javascript close() method is used to close the current window and You must write window.close() to ensure that this command is associated with a window object and not some other JavaScript object.
26.Explain “use strict” ?
Ans:“use strict” is a javascript directive that is introduced in Es5 and the purpose of using “use strict” directive is to enforce the code is executed in strict mode. In strict mode we can’t use a variable without declaring it and “use strict” is ignored by earlier versions of Javascript
27. How to get the last index of a string in Javascript ?
Ans:
var myString="JavascriptQuestions"; console.log(myString.length-1);
28. What does the instanceof operator do ?
Ans:In Javascript instanceof operator checks whether an object is an instance of a class or not:
Square.prototype = new Square(); console.log(sq instanceof Square); // true
29. Explain Promise in JavaScript ?
Ans: Promise is an object in JavaScript which is used to produce a value that may give result in the future and the value can be resolved value or it can be a reason which tells why the value is not resolved. A promise can be of three states these are:
- Fulfilled: The operation is completed and the promise has a specific value.
- Rejected: The operation is failed and promise has a reason which shows why the operation failed.
- Pending: The operation is not fulfilled or rejected, means it has not completed yet.
30. Is it possible to do 301 redirects in Javascript ?
Ans: JavaScript entirely runs on the client machine and 301 is response code that is sent by the server as a response. So it isn't possible to do 301 Redirects In JavaScript.