An object in JavaScript is a collection of key-value pairs. Each key is a string, and each value can be any data type. Objects are used to store and manage data in a structured way.
The Prototype object in JavaScript is a fundamental part of the inheritance mechanism. Every JavaScript object has a prototype, which is another object that it inherits properties and methods from. You can add methods or properties to the prototype of a constructor function, making them available to all instances of that constructor.
The keyword "this" refers to the context in which a function is called. Its value depends on the call site of the function. In global scope, it refers to the global object (e.g., window in browsers). Inside a method, it refers to the object the method is called on. In event handlers, it refers to the element that triggered the event.
"self" is used to maintain a reference to the current object when using "this" might change due to the change in the context (e.g., within a callback function). By storing "this" in "self", you can access the original context even if it changes later.
A closure is a function that retains access to its lexical scope even when the function is executed outside that scope. Closures are useful for creating private variables, functions, and maintaining state across multiple function calls.
Class methods are defined on the class itself and can be called directly on the class (e.g., ClassName.methodName()
). Instance methods are defined within the class but outside the constructor, and they operate on instances of the class (e.g., instance.methodName()
).
The == operator compares values for equality after performing type conversion, while === compares values and types for strict equality without type conversion.
The call()
and apply()
methods are used to call functions with a given this
value and arguments. The difference is in how arguments are passed: call()
takes arguments individually, while apply()
takes an array of arguments.
Asynchronous code allows JavaScript to perform tasks without blocking the execution of other code. This is crucial for tasks like API requests, file operations, or any operation that involves waiting, allowing the application to remain responsive.
JavaScript performance issues often arise from inefficient algorithms or excessive DOM manipulations. For example, repeatedly querying and updating the DOM can cause slow rendering. Optimizing performance might involve techniques like batching DOM updates or using virtual DOM frameworks.
A class is defined using the class
keyword followed by the class name. The constructor is a special method used to initialize instances of the class. Example:
class MyClass {
constructor(value) {
this.value = value;
}
}
JavaScript does not support function overloading in the traditional sense. Instead, you can use a single function with variable arguments and type checking to mimic overloading behavior.
Scope determines where variables are accessible. JavaScript has function scope (variables declared within a function are not accessible outside) and block scope (variables declared with let
and const
within a block are accessible only within that block).
JavaScript does not have true associative arrays; instead, objects are used as associative arrays where keys are strings (or symbols) and values can be of any type. Arrays with numeric indices are used for ordered data.
Inheritance in JavaScript can be achieved using prototypes or the class
syntax. With class
, you use the extends
keyword to create a subclass that inherits from a parent class.
Two-way data binding synchronizes the model and view automatically in both directions, while one-way data flow allows data to flow in only one direction (from the model to the view). Two-way binding can simplify synchronization but may be harder to debug, while one-way data flow is often used in frameworks like React for better control.
Provide specific code snippets here for answers.
To empty an array, you can set its length to 0, use the splice()
method, or assign a new empty array to it. Example:
let arr = [1, 2, 3];
arr.length = 0;
Provide specific function declarations here for answers.
The output of "1"+2+3
will be "123"
, as JavaScript treats the first "1"
as a string and concatenates the other values. The output of 1+2+"3"
will be "33"
, as the result of 1+2
is 3
, which is then concatenated with the string "3"
.
You can show a progress bar by manipulating the DOM and CSS to display a visual indicator. Example:
// HTML
// JavaScript
let xhr = new XMLHttpRequest();
xhr.onprogress = function(event) {
if (event.lengthComputable) {
let percentComplete = (event.loaded / event.total) * 100;
document.getElementById('progress-bar').style.width = percentComplete + '%';
}
};
xhr.open('GET', 'file.txt', true);
xhr.send();
A namespace is an object used to group related functions and variables to avoid naming collisions. Example:
var MyNamespace = MyNamespace || {};
MyNamespace.myFunction = function() {
// code
};
undefined
is a primitive value indicating that a variable has been declared but not initialized. An object
is a complex data type that can hold multiple values in key-value pairs.
You can convert a JSON object to a string using JSON.stringify()
. Example:
let obj = { name: "John", age: 30 };
let jsonString = JSON.stringify(obj);
You can convert a JSON string to an object using JSON.parse()
. Example:
let jsonString = '{"name": "John", "age": 30}';
let obj = JSON.parse(jsonString);
You can convert a string to lowercase using toLowerCase()
. Example:
let str = "Hello World";
let lowerStr = str.toLowerCase();
You can modify the URL using the history.pushState()
or history.replaceState()
methods without reloading the page. Example:
history.pushState({}, '', '/new-url');
You can open a URL in a new tab using window.open()
. Example:
window.open('https://www.example.com', '_blank');
var
declares a variable that is function-scoped or globally-scoped and can be re-declared and updated. let
declares a block-scoped variable that can be updated but not re-declared in the same scope. const
declares a block-scoped variable that cannot be updated or re-declared; it must be initialized at the time of declaration.
Yes, you can use JavaScript to change meta-tags dynamically. Example:
let meta = document.querySelector('meta[name="description"]');
meta.setAttribute('content', 'New Description');