JavaScript Interview Questions
JavaScript Interview Questions
● Primitive Types:
○ Number: Represents both integer and floating-point numbers.
○ String: Represents text enclosed in quotes.
○ Boolean: Represents true or false values.
○ Undefined: Represents a variable that has been declared but not assigned a
value.
○ Null: Represents the intentional absence of any object value.
○ Symbol: A unique and immutable primitive value used as an identifier for object
properties.
● Reference Types:
○ Object: A collection of key-value pairs, including arrays and functions.
● Undeclared variables: Variables that have not been declared at all, leading to a
ReferenceError if accessed. For example, console.log(x) where x was never
declared.
● Undefined variables: Variables that have been declared but not assigned a value. For
example, let x; console.log(x); will output undefined.
javascript
Copy code
const newElement = document.createElement('div');
newElement.textContent = 'Hello, World!';
document.body.appendChild(newElement);
12. What are global variables? How are these variables declared, and what are the
problems associated with them?
Global variables are accessible from any part of the code, including inside functions. They can
be declared using the var, let, or const keywords outside of any function. Problems
associated with global variables include:
javascript
Copy code
const obj = { name: 'Alice', age: 25 };
delete obj.age; // The age property is removed from the object
javascript
Copy code
const name = prompt("Please enter your name:");
17. Explain the working of timers in JavaScript. Also explain the drawbacks of using the
timer, if any.
Timers in JavaScript can be created using setTimeout() and setInterval().
Drawbacks:
● Inaccuracy: Timers may not execute exactly on time due to the single-threaded nature
of JavaScript.
● Resource consumption: Using many intervals can lead to performance issues.
● ViewState: Used to preserve page and control values between postbacks. It is stored in
a hidden field on the page and sent back and forth between the server and the client.
● SessionState: Maintains user-specific data across multiple pages. It is stored on the
server and can be accessed from any page during a user's session.
javascript
Copy code
document.getElementById('myForm').submit();
javascript
Copy code
const result = '5' + 3; // result is '53' (string)
javascript
Copy code
const input = document.getElementById('fileInput');
input.addEventListener('change', function(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
console.log(e.target.result); // Read file content
};
reader.readAsText(file);
});
For writing files, you can create a downloadable link with a Blob:
javascript
Copy code
const blob = new Blob(['Hello, World!'], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'hello.txt';
document.body.appendChild(link);
link.click();
4o mini
You said:
Answer rest of the question
ChatGPT said:
ChatGPT
javascript
Copy code
const binaryString = "1010";
const decimalNumber = parseInt(binaryString, 2); // Converts binary to
decimal
console.log(decimalNumber); // Output: 10
26. Explain how to detect the operating system on the client machine?
You can detect the operating system using the navigator.userAgent property. Here’s an
example:
javascript
Copy code
const userAgent = navigator.userAgent;
let os = "Unknown OS";
console.log(os);
Confirmation box: Displays a message and OK and Cancel buttons, returning true or false
based on user action.
javascript
Copy code
const confirmed = confirm("Are you sure?");
Prompt box: Displays a message with an input field for user input.
javascript
Copy code
const userInput = prompt("Please enter your name:");
28. What is the difference between an alert box and a confirmation box?
● Alert Box: Used to display a message to the user. It does not return any value.
● Confirmation Box: Used to confirm an action. It returns true if the user clicks OK and
false if the user clicks Cancel.
html
Copy code
<a href="javascript:void(0);" onclick="myFunction()">Click me</a>
31. What is the ‘Strict’ mode in JavaScript and how can it be enabled?
Strict mode is a feature in JavaScript that helps catch common coding errors and "unsafe"
actions, such as assigning values to undeclared variables. It can be enabled by adding "use
strict"; at the beginning of a script or a function.
javascript
Copy code
"use strict";
let x = 3.14; // This is fine
undeclaredVar = 5; // This will throw an error in strict mode
javascript
Copy code
const checkbox = document.getElementById('myCheckbox');
const isChecked = checkbox.checked; // true if checked, false
otherwise
javascript
Copy code
function createCounter() {
let count = 0; // Private variable
return function() {
count++;
return count; // Closure
};
}
const counter = createCounter();
console.log(counter()); // 1
console.log(counter()); // 2
javascript
Copy code
greet.apply(obj, ['Hi']); // Output: Hi, Alice
html
Copy code
<a href="page.html" target="myFrame">Open in Frame</a>
javascript
Copy code
window.frames['myFrame'].location.href = 'page.html';
javascript
Copy code
const myArray = [[]]; // An empty array within another array
39. How many ways can an HTML element be accessed in JavaScript code?
HTML elements can be accessed in several ways:
● getElementById: document.getElementById('elementId')
● getElementsByClassName:
document.getElementsByClassName('className')
● getElementsByTagName: document.getElementsByTagName('tagName')
● querySelector: document.querySelector('.className') or
document.querySelector('#elementId')
● querySelectorAll: document.querySelectorAll('selector')
● innerHTML: Returns or sets the HTML content (including tags) of an element. It can
manipulate HTML structure.
javascript
Copy code
element.innerHTML = '<span>New Content</span>'; // Sets HTML content
● innerText: Returns or sets the plain text content of an element, ignoring any HTML tags.
javascript
Copy code
element.innerText = 'New Content'; // Sets plain text content