Sat Jul 21 2018
JavaScript interview questions
In the middle 90's, when word JavaScript came in most of the people mind that it was only for validations and animation in web pages. Now the time has changed, JavaScript has been evolved as a programming language giant. JavaScript nowadays used in more places than ever before. It's a great language with a dark past and it suffers from its legacy. Microsoft’s Internet Explorer supported its very own interpreted script language, called JScript. Now JavaScript becomes more and more ubiquitous as it's accumulating developers mindshare. The companies take job interviews to hire a good JavaScript programmer and they always are ready to pay for the right candidate. To crack those interviews, here we are going to highlight some essential JavaScript related interview questions that give you an idea of the type of questions which may be asked in interviews. So, let's check out the questions answers -
Q. What is JavaScript?
Ans. JavaScript is a client-side as well as the server-side scripting language that can be inserted into HTML pages and is understood by web browsers. JavaScript is also an Object-based Programming language.
Q. What is JavaScript Data Types?
Ans. The JavaScript Data types are Number, String, Boolean, Function, Object, Undefined.
Q. What is negative infinity?
Ans. Negative Infinity is a number in JavaScript which can be derived by dividing the negative number by zero.
Q. Which company developed JavaScript?
Ans. Netscape is the software company who developed JavaScript.
Q. What are Frames in JavaScript?
Ans. Frames allow you to divide the page into several rectangular areas and to display a separate document in each rectangle. Each of those rectangles is called a "frame".
Q. What are undeclared and undefined variables?
Ans. Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read the value of an undeclared variable, then a runtime error is encountered. And undefined variables are declared in the program but have not been given any value. If the program tries to read the value of an undefined variable, an undefined value is returned.
Q. Is there any difference between JavaScript and Java?
Ans. Yes, the main difference between Java and JavaScript is that Java is a complete type of programming language while JavaScript is a programming language which is coded. JavaScript can be introduced and put into HTML pages. Other than this, JavaScript and Java are designed for completely different purposes. The programming language like the C++ and C are all part of Java which in turn is actually an object-oriented programming structure. On the other hand, JavaScript is a client-oriented scripting programming language.
Q. What are global variables?
Ans. Global variables are those that are available throughout the length of the code, that is, these have no scope. The var keyword is used to declare a local variable or object. If the var keyword is omitted, a global variable is declared.
Q. What are the JavaScript Operators?
Ans. JavaScript addition operator ( + ) serves two main purposes in JavaScript. The first is to perform a simple addition to the numbers. And the second operation is to perform a string concatenation (combine the strings).
Q. What are JavaScript Statements?
Ans. In JavaScript, there are different statements -
-
Block statement - JavaScript block statements are the curly braces ( { } ), you see everywhere. They are used to establish the code, which is to be compartmentalized into a specific function or a statement. The lines of the code inside of a block statement are often intended to represent that they are the part of the block statement.
-
Break statement - JavaScript break statement is used to terminate a loop, switch or label the statement from further processing. Apply it, if want to force one of those types of statements to stop the processing.
-
Continue statement - JavaScript continues statement is used to bypass the specified iterations of a loop so that the code in the loop statement does not execute for the specified iterations and moves on to the next.
-
Do...while statement - JavaScript do...while statement is an inverted while statement. It will execute the code as long as the while condition returns a value of true.
-
For statement - JavaScript for statement is a loop mechanism, which will execute code as long as the condition evaluation continues to be true.
Q. What is a prompt box?
Ans. 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.
Q. What is screen object in JavaScript?
Ans. Answer: Screen object helps in getting the information from the user's screen. We can get the width, height, color Depth etc. of the user's screen. This information will be helpful in setting the size of the images, page to be displayed in the Browser.
Q. How can you submit a form using JavaScript?
Ans. To submit a form using JavaScript use
document.form[0].submit();
document.form[0].submit();
Q. How to read and write a file using JavaScript?
Ans. There are two ways to read and write a file using JavaScript -
Using JavaScript extensions.
Using a web page and ActiveX objects.
Q. Is there any difference between JavaScript and JScript?
Ans. No, Netscape provided the JavaScript language. But, Microsoft changed the name and called it JScript to avoid the trademark issue. In other words, you can say JScript is the same as JavaScript, but it is provided by Microsoft.
Q. What are the types of comments in JavaScript?
Ans. In JavaScript, the comments are used for skipping that statement from execution. Using the comments, we make the code more readable and understandable for anyone. Code functionality is clearer, using the comments. The comments are -
-
Single Line Comment - When we want to comment out a single statement, a single line comment is used. It starts with "//". Using this, we can comment out an entire line. This line is ignored by JavaScript.
-
Multi-line comment - A multi-line comment is used with a group or the code block, which you want to comment out. A multi-line comment starts with /* and ends with */. The code block between this is skipped by JavaScript.
Q. What is BOM?
Ans. BOM stands for Browser Object Model. It provides interaction with the browser. The default object of the browser is a window.
Q. What is DOM?
Ans. DOM stands for Document Object Model.
Q. What is the use of document object?
Ans. A document object represents the HTML document. It can be used to access and change the content of HTML.
Q. How to create objects in JavaScript?
Ans. There are 3 ways to create an object in JavaScript - By object literal; By creating an instance of Object; By Object Constructor.
Q. How to create an array in JavaScript?
Ans. There are 3 ways to create an array in JavaScript - By array literal; By creating an instance of Array; By using an Array constructor.
Q. Is JavaScript a case-sensitive language?
Ans. Yes! JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.
Q. How many types of functions JavaScript supports?
Ans. A function in JavaScript can be either named or anonymous.
Q. How to define an anonymous function?
Ans. An anonymous function can be defined in a similar way as a normal function but it would not have any name.
Q. What is arguments object in JavaScript?
Ans. JavaScript variable arguments represent the arguments passed to a function.
Q. What is the purpose of 'this' operator in JavaScript?
Ans. JavaScript famous keyword 'this' always refers to the current context.
Q. What is callback?
Ans. A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered.
Q. What is closure?
Ans. Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope.
Q. Which built-in method returns the character at the specified index?
Ans. charAt() method returns the character at the specified index.
Q. Which built-in method returns the length of the string?
Ans. length() method returns the length of the string.
Q. Which built-in method removes the last element from an array and returns that element?
Ans. pop() method removes the last element from an array and returns that element.
Q. Which built-in method returns the characters in a string beginning at the specified location?
Ans. substr() method returns the characters in a string beginning at the specified location through the specified number of characters.
Q. Which built-in method returns the string representation of the number's value?
Ans. toString() method returns the string representation of the number's value.
Q. How typeof operator works?
Ans. The typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand. The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value and returns true or false based on the evaluation.
Q. Can you access Cookie using javascript?
Ans. JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookie or cookies that apply to the current web page.
Q. How to delete a Cookie using JavaScript?
Ans. Sometimes you will want to delete a cookie so that subsequent attempts to read the cookie return nothing. To do this, you just need to set the expiration date to a time in the past.
Q. How to print a web page using javascript?
Ans. JavaScript helps you to implement this functionality using print function of the window object. The JavaScript print function window.print() will print the current web page when executed.
These are not only questions that you will be asked in the interviews. Actually, it only depends on employers what they want to ask you and how they will examine you. Hope this article successfully guided you and helped you to understand the level of interview questions regarding JavaScript. Best of luck for the future! Thank you!