Today, JavaScript is a programming language which you can be used in place of C#, Java, Php or any other object-oriented languages. JavaScript can run in a browser, server-side or on any device using a program known as JavaScript Engine. This article contains most commonly asked interview questions and answers in a JavaScript or front-end technologies Interview.
What is JavaScript?
Ans. JavaScript is an object-based programming language, mostly it used as a client-side programming language with the HTML page to add some behavior for it.JavaScript initially created as a browser only language, but now it can be executed on the server or any client which has a JavaScript Engine. The product like Node.js, MongoDB, jaggery.js, ASP and many more uses server-side JavaScript.In the browser, JavaScript can do many things as given below:- Manipulating the HTML element.
- React to a user action, like running some event while user clicks on the mouse or by using the keyboard.
- Send request to the remote server.
- Downloading and uploading the files.
- Get and Set cookies and handling the client-side storage (local and session storage).
Major Advantage of using the JavaScript- Full integration with HTML/CSS .
- Supported by all major browser which is enabled by default.
What is ECMAScript?
Ans. ECMAScript is a scripting language standardized by ECMA International in ECMA-262. Languages like ActionScript, JavaScript, and many more scripting languages are used ECMAScript, among these JavaScript is a well know client-side scripting language and an implementation of ECMAScript, since the standard was published. The latest version is ECMAScript6.What are the data types supported by JavaScript?
Ans. JavaScript variables are dynamically typed, which means there is a data type but it will not bound to a particular type, example while initializing the variable it can be string type but later It can also assign to a numeric value.- Undefined
- Null
- Boolean
- Object
- String
- Symbol
- Number
What is the difference between undefined and not defined?
Ans. Considers below example- var x;
- console.log(x);
- console.log(y)
What is the use of typeof operator?
Ans. The typeof is a unary operator which means it takes a single operand in a statement or expression, it is used to check the data type of its operand in the form of a string.- var x=10;
- console.log(typeof (x))
It will print number in the console- var x = 10;
- console.log(typeof (x) == 'number')
From the above code if the typeof x is number, so from the expression it will print true in the console.What is the instanceof operator?
Ans. instanceof operator checks whether the object is an instance of a class or not.- function Country(name){this.name=name};
- var country = new Country("India");
- console.log(country instanceof Country) // return true
It will also consider inheritance- let arr = ['apple', 'orange', 'grapes'];
- console.log(arr instanceof Array); //prints true in console
- console.log(arr instanceof Object); //prints true in console
arr is an array, but it also belongs to object, because array prototypal inherits from object.What is strict mode?
Ans. “use strict” is not a statement but a literal expression which is supported by ECMAScript version 5. This statement instruct browser to use the strict mode, which is a safer future in JavaScript. It will eliminate some JavaScript silent errors.Example- "use strict";
- x = 10; // this will give error
The above statement will give an error because in strict mode the variable should be declared before it is used.The “use strict” expression can be in global scope as well as local scopeGlobal scope- const employee = { name: "Ram", age: 25 }
- employee.name = "Raju" // it is possible
- use strict";
- x = 10; // this will give error
local scope- x = 10; // This will not give error.
- myFunction();
- function myFunction() {
- "use strict";
- y = 15; // This will give error
- }
Explain String in JavaScript?
Ans. The group of character or textual data is called string, in JavaScript, there is no separate type for the character, even a single character will be stored as a string. In JavaScript, the string can be enclosed with single quotes or double quotes- var str = 'hello';
- console.log(str);//print hello
What are the differences between search() and indexOf() ?
Ans: The differences between search and indexOf methods are given below:search()indexOf()It is used to find a specified value and returns the position of the match, the value can be a string or regular expressionIt is used to find a specified value and returns the position of the match, the value should be a string, it won’t accept a regular expression- var m = /e/;
- var m = /e/;
- var str = "apple";
- str.search(m)//return 4
- var str = "apple";
- str.indexOf(m)//return -1
What are the differences between indexOf() and lastIndexOf() ?
Ans: The differences between indexOf and lastIndexOf methods are given below:indexOf()lastIndexOf()It will return the index of the first occurrence of specific text in a stringIt will return the index of the last occurrence of specific text in a string- var str = 'Hello find me test me';
- str.indexOf('me') // return 11
- var str = 'Hello find me test me';
- str.lastIndexOf('me') // return 19
What are the differences between substr() and substring()?
Ans: The differences between substr and substring methods are given below:substr()substring()It is used to return the characters in a string beginning at the specified index and returns the number of characters based on length providedIt is used to return the characters in a string beginning at the specified index and returns the number of characters based on length provided-1- var x = "hello";
- console.log((x.substr(1, 4) == "ello"))
It will print true in the log- var x = "hello";
- console.log((x.substring(1, 4) == "ello"))
It will print false in the log- var x = "hello";
- console.log((x.substring(1, 5) == "ello"))//print true in console
What are the differences between array and object?
Ans: The differences between array and object are given below:ArrayobjectThe array uses the numbered indexes to access the element in itThe object uses the named indexes to access the members in itYou should use an array when you want the element name to be numberYou should use an object when you want the element name to be a stringIt is an ordered collectionIt is a collection of unorder propertiesWhat is the Self-Executing Function?
Ans. The self-executing function will execute right after it has been defined. The advantage of using it is, it will execute the code without declaring any global. Mostly it will be used to attach event listeners to DOM elements and another initialization work.- (function ()
- {
- //function body
- })();
What is the arrow function?
Ans. The arrow function will support in JavaScript only after ES6 or above, it is a short way to write function expression. The conventional way of writing a function- function add(a, b) {
- return a + b;
- }
- console.log(add(1, 2));//3
Using arrow function- add = (a, b) => {
- return a + b
- }
- console.log(add(1, 2));//3
How to find the browser which is running the web page?
Ans. The window object navigator is used to find the browser which is currently running the web application.- var browsername = navigator.appName;
- console.log(browsername);
How to redirect the user to a new page?
Ans. We can use the window object location to redirect the user to the new page- window.location.href="https://www.dotnettricks.com/"
What is the output of below code?
- var num = "10";
- (function () {
- console.log("Original Number " + num);
- var num = "50";
- console.log("New Number " + num);
- })();
Ans. Original Number undefinedNew Number 50Reason: You will expect the original number will take the value from the outer scope, but the salary value was undefined, because of hoisting.What is DOM?
Ans. DOM is a W3C (World wide web consortium) standard, when the HTML page loads in the browser, the browser creates the DOM (Document object model). It defines the HTML element as an object and allows scripts to dynamically manipulate the content, and the structure of the document.HTML:- <!DOCTYPE html>
- <html lang="en">
- <body>
- <h1>Document Object Model</h1>
- </body>
- </html>
In DOM, every HTML is an object, Nested tag are “children”, the text inside a <h1> is an object as wellThe DOM Tree of objectsThe DOM represents HTML as a tree structure of tags. Here’s how it looks in browser inspect elementWhat is BOM?
Ans. BOM (Browser Object Model) which provides interaction with the browser, the default object of the browser is a window. Various property provided by windows is a document, history, screen, location, navigator.What are the different ways to access HTML element in JavaScript?
Ans. The following DOM Methods are used to capture the HTML element and manipulate it.- getElementById('idname') - > This function is used to select the HTML element based on ID
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title></title>
- </head>
- <body>
- <label id="myelement"></label>
- <script>
- document.getElementById('myelement').innerHTML = '<h3> Welcome </h3>'
- </script>
- </body
- </html>
- getElementsByClassName('className') - > This function is used to select the HTML elements based on the class name in DOM, it will return all matched HTML element with respect to the class name.
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title></title>
- </head>
- <style>
- .lblMsg {
- color: #000;
- }
- </style>
- <body>
- <label id="myelement" class="lblMsg"></label>
- <script>
- document.getElementsByClassName('lblMsg')[0].innerHTML = '<h3> Welcome </h3>'
- </script>
- </body>
- </html>
- getElementsByTagName(‘HTMLtagname’) - > This function is used to select the HTML elements based on the Tag name in DOM, it will return all matched HTML element with respect to the Tag name.
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title></title>
- </head>
- <style>
- .lblMsg {
- color: #000;
- }
- </style>
- <body>
- <label id="myelement" class="lblMsg"></label>
- <script>
- document.getElementsByTagName('label')[0].innerHTML = '<h3> Welcome </h3>'
- </script>
- </body>
- </html>
Now in the console, we will get a message x is ‘undefined’ which means the variable is declared and memory is created but the value is not assigned to it.
In this case, you will get a message like ‘not defined’ because the variable y is not created, and memory is not allocated for it and we try to reference the variable.
No comments:
Post a Comment