TQL

logo

Welcome To Ask Me

1. What is JavaScript?

JavaScript is a scripting language. It is different from Java language. It is object-based, lightweight, cross-platform translated language. It is widely used for client-side validation. The JavaScript Translator (embedded in the browser) is responsible for translating the JavaScript code for the web browser

2. List some features of JavaScript.

Some of the features of JavaScript are:

  • Lightweight
  • Interpreted programming language
  • Good for the applications which are network-centric
  • Complementary to Java
  • Complementary to HTML
  • Open source
  • Cross-platform

3. List some of the advantages of JavaScript.

Some of the advantages of JavaScript are:

  • Server interaction is less
  • Feedback to the visitors is immediate
  • Interactivity is high
  • Interfaces are richer

4. Define a named function in JavaScript.

The function which has named at the time of definition is called a named function. For example

 
  1. function msg()  
  2. {  
  3.   document.writeln(“Named Function”);  
  4. }  
  5. msg();  

5. What are JavaScript Data Types?

There are three major Data types in JavaScript.

  • Primitive
    • Numbers
    • Strings
    • Boolean
    • Symbol
  • Trivial
    • Undefined
    • Null
  • Composite
    • Objects
    • Functions
    • Arrays

6. Which symbol is used for comments in JavaScript?

Comments are used to prevent the execution of statements. Comments are ignored while the compiler executes the code. There are two types of symbols used to represent comments in JavaScript:

 
  • Double slash: It is known as a single-line comment.
// Single line comment
  • Slash with Asterisk: It is known as a multi-line comment.
/* 
Multi-line comments
...
*/

7. What is the use of the isNaN function?

The number isNan function in JavaScript is used to determine whether the passed value is NaN (Not a number) and is of the type “Number”. In JavaScript, the value NaN is considered a type of number. It returns true if the argument is not a number, else it returns false.

8. What are undeclared and undefined variables?

  • Undefined: It occurs when a variable has been declared but has not been assigned any value. Undefined is not a keyword.
  • Undeclared: It occurs when we try to access any variable which is not initialized or declared earlier using the var or const keyword. If we use ‘typeof’ operator to get the value of an undeclared variable, we will face the runtime error with the return value as “undefined”. The scope of the undeclared variables is always global.

9. Write a JavaScript code for adding new elements dynamically.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
  
<body>
    <button onclick="create()">
        Click Here!
    </button>
  
    <script>
        function create() {
            let geeks = document.createElement('geeks');
            geeks.textContent = "Geeksforgeeks";
            geeks.setAttribute('class', 'note');
            document.body.appendChild(geeks);
        }
    </script>
</body>
</html>

10. What are global variables? How are these variables declared, and what are the problems associated with them?

In contrast, global variables are the variables that are defined outside of functions. These variables have a global scope, so they can be used by any function without passing them to the function as parameters. 

Example: 

 
let petName = "Rocky"; //Global Variable
myFunction();
  
function myFunction() {
    document.getElementById("geeks").innerHTML
        = typeof petName + "- " +
        "My pet name is " + petName;
}
  
document.getElementById("Geeks")
    .innerHTML = typeof petName + "- " +
    "My pet name is " + petName;

11. How to delete property-specific values?

The delete keyword is used to delete the whole property and all the values at once like

let gfg={Course: "DSA", Duration:30};
delete gfg.Course;

12. What is a prompt box?

It is used to display a dialog box with an optional message prompting the user to input some text. It is often used if the user wants to input a value before entering a page. It returns a string containing the text entered by the user, or null.

13. What is the ‘this’ keyword in JavaScript?

Functions in JavaScript are essential objects. Like objects, they can be assigned to variables, passed to other functions, and returned from functions. And much like objects, they have their own properties. ‘this’ stores the current execution context of the JavaScript program. Thus, when it is used inside a function, the value of ‘this’ will change depending on how the function is defined, how it is invoked, and the default execution context.

14. What is the difference between ViewState and SessionState?

  • ViewState: It is specific to a single page in a session.
  • SessionState: It is user specific that can access all the data on the web pages.

15. How can you submit a form using JavaScript?

You can use document.form[0].submit() method to submit the form in JavaScript

16. How to use external JavaScript file?

I am assuming that js file name is message.js, place the following script tag inside the head tag.

  1. <script type=“text/javascript” src=“message.js”></script>  

17. What is DOM? What is the use of document object?

DOM stands for Document Object Model. A document object represents the HTML document. It can be used to access and change the content of HTML.

18. What is the use of window object?

The window object is created automatically by the browser that represents a window of a browser. It is not an object of JavaScript. It is a browser object.

The window object is used to display the popup dialog box. Let’s see with description.

MethodDescription
alert()displays the alert box containing the message with ok button.
confirm()displays the confirm dialog box containing the message with ok and cancel button.
prompt()displays a dialog box to get input from the user.
open()opens the new window.
close()closes the current window.
setTimeout()performs the action after specified time like calling function, evaluating expressions.

19. What is the use of history object?

The history object of a browser can be used to switch to history pages such as back and forward from the current page or another page. There are three methods of history object.

  1. history.back() – It loads the previous page.
  2. history.forward() – It loads the next page.
  3. history.go(number) – The number may be positive for forward, negative for backward. It loads the given page number.

 

20. What is the difference between == and ===?

The == operator checks equality only whereas === checks equality, and data type, i.e., a value must be of the same type.

21. How to write HTML code dynamically using JavaScript?

The innerHTML property is used to write the HTML code using JavaScript dynamically. Let’s see a simple example:

document.getElementById(‘mylocation’).innerHTML=“<h2>This is heading using JavaScript</h2>”;   

22. How to create an array in JavaScript?

There are 3 ways to create an array in JavaScript.

  1. By array literal
  2. By creating an instance of Array
  3. By using an Array constructor

Let’s see a simple code to create an array using object literal.

 
  1. var emp=[“Shyam”,”Vimal”,”Ratan”];    

23. Difference between Client side JavaScript and Server side JavaScript?

Client-side JavaScript comprises the basic language and predefined objects which are relevant to running JavaScript in a browser. The client-side JavaScript is embedded directly by in the HTML pages. The browser interprets this script at runtime.

Server-side JavaScript also resembles client-side JavaScript. It has a relevant JavaScript which is to run in a server. The server-side JavaScript are deployed only after compilation.

24. In which location cookies are stored on the hard disk?

The storage of cookies on the hard disk depends on the OS and the browser.

The Netscape Navigator on Windows uses a cookies.txt file that contains all the cookies. The path is c:\Program Files\Netscape\Users\username\cookies.txt

The Internet Explorer stores the cookies on a file username@website.txt. The path is: c:\Windows\Cookies\username@Website.txt.

25. What's the difference between event.preventDefault() and event.stopPropagation() methods in JavaScript?

In JavaScript, the event.preventDefault() method is used to prevent the default behavior of an element.

For example: If you use it in a form element, it prevents it from submitting. If used in an anchor element, it prevents it from navigating. If used in a contextmenu, it prevents it from showing or displaying.

On the other hand, the event.stopPropagation() method is used to stop the propagation of an event or stop the event from occurring in the bubbling or capturing phase.

26. How can you check if the event.preventDefault() method was used in an element?

When we use the event.defaultPrevent() method in the event object returns a Boolean indicating that the event.preventDefault() was called in a particular element.

27. What is the difference between View state and Session state?

“View state” is specific to a page in a session whereas “Session state” is specific to a user or browser that can be accessed across all pages in the web application.

28. What is the use of debugger keyword in JavaScript?

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 can start the flow of execution manually. If an exception occurs, the execution will stop again on that particular line.. For example:

 
  1. function display()  
  2. {  
  3. x = 10;    
  4. y = 15;    
  5. z = x + y;    
  6. debugger;    
  7. document.write(z);    
  8. document.write(a);     
  9. }     
  10. display();  

29. What is the use of a Date object in JavaScript?

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.

 
  1. function display()  
  2. {  
  3.   var date=new Date();    
  4. var day=date.getDate();    
  5. var month=date.getMonth()+1;    
  6. var year=date.getFullYear();    
  7. document.write(“<br>Date is: “+day+”/”+month+”/”+year);    
  8. }  
  9. display();  

30. Define closure.

In JavaScript, we need closures when a variable which is defined outside the scope in reference is accessed from some inner scope.

 
  1. var num = 10;  
  2. function sum()   
  3. {  
  4. document.writeln(num+num);  
  5. }   
  6. sum();  

Interview Questions