The output would be 5. When we use the delete operator to delete an array element it doesn't unset the element in that position of the array, but sets it to undefined instead. So, the array length is not affected by the delete operation. This holds true even if you deleted all elements of an array using the delete operator.
"}},{"@type":"Question","name":"What is the difference between == and ===?","acceptedAnswer":{"@type":"Answer","text":"
The == operator checks equality only, whereas === checks equality and data type i.e. the values must be of the same type.
"}},{"@type":"Question","name":"What does the isNaN() function do?","acceptedAnswer":{"@type":"Answer","text":"The isNan() function returns true if the variable value is not a number."}},{"@type":"Question","name":"What is the result of \"10\"+20+30 in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"
The result is 102030 because when using the + operator with a string, it acts as a string concatenation operator (not binary +). To make it work as expected, you should parse the \"10\" to integer before doing the +.
"}},{"@type":"Question","name":"How does Array() differ from [] while creating a JavaScript array?","acceptedAnswer":{"@type":"Answer","text":"
Both the Array() and [] work almost the same in JavaScript.
\n
If we use them as is (i.e. without any argument) to create an array object, then they will result in an array object of zero length. Even if we pass a string or a list of strings as arguments, the result will be similar.\n
However, they differ when the input argument is of integer type. In that case, the statement will create an uninitialized array of size n. Whereas, the [n] statement will create an array of size 1 and assign n as the value of the first element.
"}},{"@type":"Question","name":"What is the difference between declaring a function in the two formats below?
var foo = function() {
/* Some code */
};
function bar() {
/* Some code */
};
","acceptedAnswer":{"@type":"Answer","text":"The main difference is that foo is defined at run-time whereas bar is defined at parse time. E.g.
/* Run-Time function declaration*/ foo(); /* Calling foo here will throw an error */ var foo = function() {
console.log(\"Hi I am inside Foo\");
};
/* Parse-Time function declaration */ bar(); /* Call bar function here, It will not give an Error */ function bar() {
console.log(\"Hi I am inside Foo\");
};
"}},{"@type":"Question","name":"What is Scope in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"
The scope determines the accessibility of variables, objects, and functions in particular part of your code.
In JavaScript, the scope can be of two types.
1. Global Scope
2. Local Scope
"}},{"@type":"Question","name":"How to call other class methods?","acceptedAnswer":{"@type":"Answer","text":"
Using call() and apply() method we can use methods from different context to the current context. It is really helpful for code reusability and context binding.
call(): is used to call a function with a given this value and arguments provided individually.
apply(): is used to call a function with a given this value and arguments provided as an array.
"}},{"@type":"Question","name":"Explain the difference between class inheritance and prototypal inheritance.","acceptedAnswer":{"@type":"Answer","text":"
Class Inheritance: A constructor function instantiates an instance via the \"new\" keyword. This new instance inherits properties from a parent class.
Prototypal Inheritance: An instance is created by cloning an existing object that serves as a prototype. Instances are typically instantiated via factory functions, object literals, or Object.create(). Instances may be composed from many different source objects, allowing for easy selective inheritance.
"}},{"@type":"Question","name":"What is prototype property in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"Every JavaScript function has a prototype property (by default this property is null), that is mainly used for implementing inheritance. We add methods and properties to a function's prototype so that it becomes available to instances of that function."}},{"@type":"Question","name":"What is the reason for wrapping the entire content of a JavaScript source file in a function block?","acceptedAnswer":{"@type":"Answer","text":"This is a common practice used in many popular JavaScript libraries (jQuery, Node.js, etc.). It creates a closure around the entire contents of the file which makes a private namespace and thereby avoids potential name clashes between different JavaScript modules and libraries."}},{"@type":"Question","name":"Why would you use use strict at the beginning of a JavaScript source file?","acceptedAnswer":{"@type":"Answer","text":"
Using strict is a way to enforce strict parsing and error handling of the JavaScript code at runtime. This means that code errors that would have otherwise been ignored or would have failed silently, will now generate errors or throw exceptions.
Some benefits are: easier debugging; preventing accidental globals, eliminates misuse of this etc.
"}},{"@type":"Question","name":"What is the difference between a method and a function in javascript?","acceptedAnswer":{"@type":"Answer","text":"
A function is a piece of code that is called by name and it is not associated with any object, nor defined inside an object. It can be passed data to operate on (i.e. parameter) and can optionally return data (the return value). E.g.:
/* Function definition*/ function myFunc() {
/* Do some stuff; */
} myFunc();/* Calling the function */
A method is a piece of code that is called by name and is defined inside an object. It is almost identical to a function except that it is always associated with an object and operating only on data inside it.
var methodObject = {
attribute: \"xyz\",
display: function () { /* Method*/
console.log(this.attribute);
}
}methodObject.display(); /* Calling the method */
"}},{"@type":"Question","name":"What is JavaScript self-invoking anonymous function?","acceptedAnswer":{"@type":"Answer","text":"
A self-invoking anonymous function runs immediately when we define it and doesn't have a name. E.g.
(function() {
console.log(\"this will print automatically\");
})();
"}},{"@type":"Question","name":"What will the code below output?
var a = [1, 2, 3]; a[10] = 99; console.log(a[6]);
","acceptedAnswer":{"@type":"Answer","text":"When doing a[10] = 99; the JavaScript engine will set the array slots from 3 to 9 to an empty value. That would equal a declared, but not defined variable. Accordingly, doing console.log(a[6]); will print undefined."}},{"@type":"Question","name":"What will be the output of the following code?
var Employee = {
company: 'Acme'
} var employee1 = Object.create(Employee); delete employee1.company console.log(employee1.company);
","acceptedAnswer":{"@type":"Answer","text":"The above code will output Acme. For the object employee1, company is a prototype property that can't be deleted using the delete operator."}},{"@type":"Question","name":"What will be the output of the following code?
var z = 1, y = z = typeof y; console.log(y);
","acceptedAnswer":{"@type":"Answer","text":"The above code will output undefined. The order of execution with the = operator is right to left, which means typeof y will execute first and will return undefined, which will then pass to z and y. Thus, console.log(y); will print undefined."}},{"@type":"Question","name":"Consider the two functions below. Will they both return the same thing?
function foo1()
{
return {
bar: \"hello\"
};
}
function foo2()
{
return
{
bar: \"hello\"
};
}
","acceptedAnswer":{"@type":"Answer","text":"
Surprisingly, no. JavaScript is very fogiving of missed semi-colons, so when you write a statement in new line without ending it with a ;, it is automatically inserted by the engine. That will result in foo2 returning undefined, while foo1 an object.