Q1. What
are the scopes of a variable in JavaScript?
The scope of a variable is
the region of your program in which it is defined.
JavaScript variable will have only two scopes.
• Global Variables − A global variable has global scope which means it is visible everywhere in your JavaScript code.
• Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
• Global Variables − A global variable has global scope which means it is visible everywhere in your JavaScript code.
• Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
Q2.
What is JavaScript?
JavaScript is a lightweight, interpreted
programming language with object-oriented capabilities that allows you to build
interactivity into otherwise static HTML pages. The general-purpose core of the
language has been embedded in Netscape, Internet Explorer, and other web
browsers.
Q3.
What is argument objects in JavaScript & how to get the type of arguments
passed to a function?
JavaScript variable arguments represents the
arguments that are passed to a function. Using typeof operator, we can get the
type of arguments passed to a function. For example
function
func(x){
console.log(typeof x, arguments.length);
}
func(); //==> "undefined", 0
func(7); //==> "number", 7
func("1", "2",
"3"); //==> "string", 3
Q4.
How to delete a cookie using JavaScript?
If you want to delete a cookie so that
subsequent attempts to read the cookie return nothing, you just need to set the
expiration date to a time in the past. You should define the cookie path to
ensure that you delete the right cookie. Some browsers will not let you delete
a cookie if you don’t specify the path.
Now let’s move on to the next section of
JavaScript interview questions.
Q5.
In how many ways a JavaScript code can be involved in an HTML file?
There are 3 different ways in which a
JavaScript code can be involved in an HTML file:
• Inline
• Internal
• External
An inline function is a JavaScript function,
which is assigned to a variable created at runtime. You can differentiate
between Inline Functions and Anonymous since an inline function is assigned to
a variable and can be easily reused. When you need a JavaScript for a function,
you can either have the script integrated in the page you are working on, or
you can have it placed in a separate file that you call, when needed. This is
the difference between an internal script and an externalscript.
Q6.
How do JavaScript primitive/object types passed in functions?
One of the differences between the two is that
Primitive Data Types are passed By Value and Objects are passed By Reference.
• By
Value means creating a COPY of the original. Picture it like twins: they are
born exactly the same, but the first twin doesn’t lose a leg when the second
twin loses his in the war.
•
By Reference means creating an ALIAS to the original. When your Mom calls you
“Pumpkin Pie” although your name is Margaret, this doesn’t suddenly give birth
to a clone of yourself: you are still one, but you can be called by these two
very different names.
Q7.
What will be the output of the code below?
var
Y = 1;
if (function F(){})
{
y += Typeof F;</span>
}
console.log(y);
The output would be 1undefined. The if
condition statement evaluates using eval, so eval(function f(){}) returns
function f(){} (which is true). Therefore, inside the if statement, executing
typeof f returns undefined because the if statement code executes at run time,
and the statement inside the if condition is evaluated during run time.
Q8.
How to empty an Array in JavaScript?
There are a number of methods you can use to
empty an array:
Method 1 –
1 arrayList
= []
Above code will set the variable arrayList to
a new empty array. This is recommended if you don’t have references to the
original array arrayList anywhere else, because it will actually create a new,
empty array. You should be careful with this method of emptying the array,
because if you have referenced this array from another variable, then the
original reference array will remain unchanged.
Method 2 –
1 arrayList.length
= 0;
The code above will clear the existing array
by setting its length to 0. This way of emptying the array also updates all the
reference variables that point to the original array. Therefore, this method is
useful when you want to update all reference variables pointing to arrayList.
Method 3 –
1 arrayList.splice(0,
arrayList.length);
The implementation above will also work
perfectly. This way of emptying the array will also update all the references
to the original array.
Method 4 –
while(arrayList.length)
{
arrayList.pop();
}
The implementation above can also empty
arrays, but it is usually not recommended to use this method often.
Q9.
What will be the output of the following code?
var
X = { Foo : 1};
var Output = (function()
{
delete X.foo;
return X.foo;
}
)();
console.log(output);
The output would be undefined. The delete
operator is used to delete the property of an object. Here, x is an object
which has the property foo, and as it is a self-invoking function, we will
delete the foo property from object x. After doing so, when we try to reference
a deleted property foo, the result is undefined.
Q10.
How does TypeOf Operator work?
The typeof operator is used to get the data
type of its operand. The operand can be either a literalor a data structure
such as a variable, a function, or an object. It is a unary operator that is
placed before its single operand, which can be of any type. Its value is a
string indicating the data type of the operand.
0 Comments