Top 10 JavaScript Interview Questions for 2022

 


Q1) What is difference between Java and JavaScript.

Java JavaScript
Java is an OOP programming language. Java is an OOP scripting language.
It creates applications that run in a virtual machine or browser. The code is run on a browser only.
Java code needs to be compiled. JavaScript code are all in the form of text.


Q2) What are the data types supported by JavaScript?

There are 3 major datatypes in JavaScript:
  • Primitive
    • Numbers
    • Strings
    • Boolean
  • Trivial
    • Null
    • Undefined
  • Composite
    • Objects
    • Functions
    • Arrays

Q3) Is JavaScript a case-sensitive language?

Yes, JavaScript is a case sensitive language. The language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

Q4) What are the advantages of JavaScript?

Following are the advantages of using JavaScript −
  • Less server interaction − You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.
  • Immediate feedback to the visitors − They don’t have to wait for a page reload to see if they have forgotten to enter something.
  • Increased interactivity − You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.
  • Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.

Q5) Write a JavaScript code for adding new elements dynamically ?


Code:
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript code for adding new
elements dynamically
</title>
</head>
<body>
<button onclick="create()">
Click Here!
</button>
<script>
function create() {
var geeks = document.createElement('aj');
aj.textContent = "AJBlogs";
aj.setAttribute('class', 'note');
document.body.appendChild(aj);
}
</script>
</body>
</html>

Q6) What are global variables? How are these variable 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.

Code:
<script> 
     var petName = "Bruno"; //Global Variable 
     myFunction(); 
    
     function myFunction() { 
         document.getElementById("AJ").innerHTML
                      = typeof petName + "- " + 
                      "My pet name is " + petName; 
     } 
    
     document.getElementById("AJ").innerHTML
                      = typeof petName + "- " + 
                      "My pet name is " + petName; 
</script> 

Q7) Explain Hoisting in JavaScript.

Hoisting is a default behaviour of JavaScript where all the variable and function declarations are moved on top.

This means that irrespective of where the variables and functions are declared, they are moved on top of the scope. The scope can be both local and global.


Example 1:
hoistedVariable = 100;
console.log(hoistedVariable); // outputs 3 even when the variable is declared after it is initialized
var hoistedVariable;
Example 2:
hoistedFunction(); // Outputs " Hello world! " even when the function is declared after calling
function hoistedFunction(){
console.log(" Hello world! ");
}
Example 3:
// Hoisting takes place in the local scope as well
function doSomething(){
x = 33;
console.log(x);
var x;
}

Q8) Difference between "==" and "===" operators.

Both are comparison operators. The difference between both the operators is that, "==" is used to compare values whereas, "===" is used to compare both value and types.


Example 1:
var x = 2;
var y = "2";
(x == y) // Returns true since the value of both x and y is the same
(x === y) // Returns false since the typeof x is "number" and typeof y is "string"

Q9)What is npm? [Node.js]

npm stands for Node Package Manager. It is a package manager for JavaScript. npm puts modules in place so that nodes can find them. npm also manages dependency conflicts. It is used to publish, discover, install, and develop node programs.

Q10)What is DOM (Document Object Model)?

The Document Object Model (DOM) is a cross-platform programming interface that represents HTML and XML documents as nodes and objects. In simple terms, it defines the logical structure of documents and the way the documents are accessed and manipulated. DOM enables programmers to create, modify, and delete the document structure, style, and content. When a document is displayed on a browser, the content of the document must be combined with its style information. The browser converts HTML and CSS into the DOM. The DOM combines the document’s content with its style.

More Questions Coming Soon
AJ Blogs

Hello everyone, My name Arth and I like to write about what I learn. Follow My Website - https://sites.google.com/view/aj-blogs/home

Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page