Javascript interview questions: Hi Guys, if you don’t know about Commonly Asked Javascript interview
questions, and you are searching for Commonly
Asked Javascript interview questions to learn. Than you are at the right
place.
Latest Commonly Asked Javascript interview questions of 2020
The Commonly Asked JavaScript interview questions with answers for
beginners are given below.
Q1. What
is Functions in JavaScript?
A function is a block of reusable
code.
A function
allows us to write code once and use it multiple time by calling the function.
Q2. What is the syntax of
function in JavaScript?
The syntax
of function in javascript is given below:
function functionName(param1, param2,..paramn)
{
//function
statements
}
Note:
here param means parameter.
Q3. What are the main points
to remember when we creating a function in JavaScript?
Below are the main points to
remember when we creating a function in JavaScript:
1. To define a function use the function keyword, followed by the name of
the function. The name of the function should be followed by parentheses ().
2. Function parameters are optional. The
parameter names must be with in parentheses separated by commas.
Example:
function addNumbers(firstNum,
secondNum)
{
var result = firstNum + secondNum;
return result;
}
Q4. How to call the JavaScript function?
We Call the JavaScript function by specifying the name of the function and
values for the parameters if any.
var sum
= addNumbers(10, 20);
alert(sum);
Output : 30
Q5. What happens when you do not specify values for the
function parameters when calling the function?
The
parameters that are missing values are set to undefined.
Example:
function addNumbers(firstNumber,
secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}
Ex:
var sum = addNumbers(10);
alert(sum);
Output : NaN
In the
example above, we are passing 10 for the firstNumber parameter but the
secondNumber parameter is missing a value, so this parameter will be set to
undefined. When a plus (+) operator is applied between 10 and undefined we get
not a number (NaN) and that will be alerted.
Q6. What happens when you specify too many parameter
values when calling the function?
In this
case the extra parameter values are ignored.
Example:
function addNumbers(firstNumber,
secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}
var sum = addNumbers(10,
20, 30, 40);// In the example below, 30 & 40 are ignored.
alert(sum);
Output:10,20
Q7. Should a javascript function always return a value?
No, they
don't have to. It totally depends on what you want the function to do. If an
explicit return is omitted, undefined is returned automatically.
Ex:
function addNumbers(firstNumber,
secondNumber)
{ var result = firstNumber + secondNumber;
return result;
}
var sum
= addNumbers(10, 20);
document.write(sum);
Description: The function addNumbers returns the sum of two numbers. We are storing
the return value of the function in sum variable and writing it's value to the
document.
Ex:
function addNumbers(firstNumber, secondNumber)
{ var result =
firstNumber + secondNumber;
document.write(result);
}
var sum = addNumbers(10,
20);
alert(sum);
Description:
The following function does not return any value. It
simply writes the sum of two numbers to the page. However, we are assigning the
return value of addNumbers() function to sum variable. Since addNumbers()
function in this case does not have an explicit return statement, undefined
will be returned.
Q8. How to
defining functions in JavaScript?
There are different ways to define a function in JavaScript:
1. Using function declaration: First declare a function and then calling
it.
Ex: function addNumbers(firstNumber, secondNumber)
{ var result = firstNumber +
secondNumber;
return result;
}
var sum = addNumbers(10, 20);
document.write(sum); Output : 30
2. Call a function before declaring it.
Example 2 :
var sum = addNumbers(10, 20);
document.write(sum);
function addNumbers(firstNumber, secondNumber)
{
var result = firstNumber +
secondNumber;
return result;
}
Note: We can call java script function anywhere, even before the function
is declared. The above code also works fine with same result. Here we are
calling the function before it is declared.
Q9. What
is Function
Hoisting?
Function Hoisting is technique, by which JavaScript
moves all the function declarations to the top of the current scope. This is
the reason JavaScript functions can be called before they are declared.
Q10. What
is function
expression in Java script?
Function Expression allows us to define a function
using an expression (by assigning it to a variable). There are 3 different ways
of defining a function using a function expression.
1. Anonymous function expression
1. Anonymous function expression
2. Named function expression
3. Self invoking function expression
Q11. What is Anonymous function expression
in Javascript?
In Anonymous function expression, we are creating a
function without a name and assigning it to variable add. We use the name of
the variable to invoke the function.
var add = function (firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}
var sum = add(10, 20);
document.write(sum);
var add = function (firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}
var sum = add(10, 20);
document.write(sum);
Note: Functions defined using a function expression are not hoisted. So, this means a function defined
using a function expression can only be called after it has been defined while
a function defined using standard function declaration can be called both
before and after it is defined.
Ex: var sum = add(10, 20); // add() is undefined at this stage
document.write(sum);
var add = function (firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}
Ex: var sum = add(10, 20); // add() is undefined at this stage
document.write(sum);
var add = function (firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}
Q12. What is Named function expression
in Javascript?
Named function expression is similar to the example
above. The difference is instead of assigning the variable to an anonymous
function, we’re assigning it to a named function (computeFactorial).
var factorial = function computeFactorial(number)
{
if (number <= 1) { return 1;}
return number * computeFactorial(number - 1);
}
var factorial = function computeFactorial(number)
{
if (number <= 1) { return 1;}
return number * computeFactorial(number - 1);
}
Ex:
var result = factorial(5);
document.write(result);
The name of the function (i.e computeFactorial) is available only with in the same function. This syntax is useful for creating recursive functions. If you use computeFactorial() method outside of the function it raises 'computeFactorial' is undefined error.
var factorial = function computeFactorial(number)
{
if (number <= 1)
{ return 1; }
return number * computeFactorial(number - 1);
}
var result = computeFactorial(5);
document.write(result);
Output : Error - 'computeFactorial' is undefined.
var result = factorial(5);
document.write(result);
The name of the function (i.e computeFactorial) is available only with in the same function. This syntax is useful for creating recursive functions. If you use computeFactorial() method outside of the function it raises 'computeFactorial' is undefined error.
var factorial = function computeFactorial(number)
{
if (number <= 1)
{ return 1; }
return number * computeFactorial(number - 1);
}
var result = computeFactorial(5);
document.write(result);
Output : Error - 'computeFactorial' is undefined.
Q13. What is Self invoking function
expression in Javascript?
Self-invoking
function expression:
var result = (function computeFactorial(number)
{
if (number <= 1)
{ return 1; }
return number * computeFactorial(number - 1);
})(5);
document.write(result); Output : 120
Q14. What are Local and global variables in JavaScript?
var result = (function computeFactorial(number)
{
if (number <= 1)
{ return 1; }
return number * computeFactorial(number - 1);
})(5);
document.write(result); Output : 120
Q14. What are Local and global variables in JavaScript?
Local variables: These variables declared inside a function. These variables are
available only inside that function. These variables are created when a
function starts, and deleted as soon as the function completes execution.
function helloWorld()
function helloWorld()
{ var greeting = "Hello"; // The variable greeting is available
in the function
greeting = greeting + " JavaScript";
alert(greeting);
}
helloWorld();
// The variable greeting is not available outside
the function
// Error : 'greeting' is undefined
alert(greeting);
Global variables: These variables are declared outside a function. All scripts and functions on the page can access these Global variables. The lifetime of a global variable starts with it's declaration and is deleted when the page is closed.
var greeting = "Hello";
function helloWorld()
{
// The variable greeting is available
in the function
greeting = greeting + " JavaScript";
alert(greeting);
}
helloWorld();
Rule: If you assign a value to a variable that has not been declared, it will automatically become a global variable, even if it is present inside a function.
function helloWorld()
{
// The variable greeting is not
declared but a value is assigned.
// So it will automatically become a
global variable
greeting = "Hello JavaScript";
}
helloWorld();
// Variable greeting is available outside the
function
alert(greeting);
Q15. Can a local variable have the same name as a global variable?
Yes.
Changing the value of one variable has no effect on the
other.
If the variable value is changed inside a function then
the local variable gets modified.
If the variable value is changed outside a function
then the global variable gets modified.
var myalue = "This is from global Variable";
var myalue = "This is from global Variable";
function variableValue()
{ var myvalue = "This is from local
variable";
document.write(myvalue + "<br/>");
}
// This line will modify the global myvalue variable
myvalue += "!!!";
variableValue ();
document.write(myalue);
Output :
This is from local variable
This is from global Variable!!!
Sometimes, variable hoisting and local & global variable with the same name can cause unexpected behavior.
var greeting = "This is from global Variable";
helloWorld();
function helloWorld()
{
document.write(greeting);
var greeting
= "Hello from local
variable"
}
Output : undefined
At runtime due to variable hoisting, the above program would look more like as shown below.
var greeting = "This is from global Variable";
At runtime due to variable hoisting, the above program would look more like as shown below.
var greeting = "This is from global Variable";
helloWorld();
function helloWorld()
{ var greeting;
document.write(greeting);
greeting = "Hello from local variable"
}
Q16. IS Braces create scope in
JavaScript?
No, Braces do not create scope in
JavaScript.
In the following example otherNumber is a global variable though it is defined inside braces. In many
languages like C# and Java, braces create scope, but not JavaScript.
var number = 100;
if (number
> 10)
{var otherNumber
= number;}
document.write(otherNumber);
Output: 100
Final words
So Guys, This is my own Javascript interview
questions. I hope this article is helpful for you, please do share and
comment you’re thought about this all commonly asked Javascript interview questions.
Thank You!
If you find something new to
learn today from commonly asked Javascript interview
questions, then do share it with others. And, follow us on our social
media (Facebook/Twitter) accounts to see more of this.
Best,
PSCTech
0 comments:
Post a Comment