Football Tutorial

    Get Free Football Tutorial details and much more.
  • Home
  • About
  • Champion of Champions
  • Football - Tournaments
  • CONTACT
  • PRIVACY

Sunday, 29 September 2019

Frequently Asked JavaScript Interview Questions And Answers For Developer

 No comments   

Javascript interview questions: Hi Guys, if you don’t know about Frequently Asked Javascript interview questions, and you are searching for Frequently Asked Javascript interview questions to learn. Than you are at the right place.

Latest Frequently Asked Javascript interview questions of 2020
The Frequently asked JavaScript interview questions with answers for beginners are given below.

Q1. What are the difference between == and === in java script?
== Check contents are equal or not of both values.
=== Check contents as well as type of both values.
Ex:
3==='3'//false, because one is integer and other is string value.
3==="3"//false, because one is integer and other is string value.
"3"==3//true, because both have same value i.e. 3
 3===3// true, because both have same value i.e. 3

Q2. What is !! (Double Not-Operator) in Java script?
!! converts a value (null, undefined, objects etc…) to a primitive Boolean value(true/false).
Ex: alert (!!undefined); //false
alert (!!null); //false
alert (!!NaN); //false
alert (!!1); //true
alert(!!'test'); //true
alert (!!-1); //true

Q3. What is Void 0 in Javascript?
void 0 returns undefined. void 0 and undefined both are working  same.
For older browser, undefined is used. In modern browsers (which supports javascript 1.8.5) we use void 0.
var myVal = 1;
alert(myVal);  //1
void 0 is safer and can be used in place of undefined.
Other advantage of void 0 is less type than undefined.
var myVar;
alert(myVar === void 0); //true

Q4. What happens when a browser starts loading a web page?

1. At first browser starts parsing the HTML contents.
2. When the parser encounters a <script> tag that references an external JavaScript file. The parser stops parsing the HTML and the browser makes a request to download the script file. Until the download is complete, the parser is blocked from parsing the rest of the HTML on the page.
3. When the download is complete, that's when the parser will resume to parse the rest of the HTML. 
This means the page loading is stopped until all the scripts are loaded which affects user experience.

Q5. What are the different methods available in JavaScript to convert strings to numbers.
Following functions are used to convert strings to numbers:
1. parseInt()  2. parseFloat() 3. isNan()

Q6. How to use parseInt() function?
function addNumbers()
{
    var firstNumber = parseInt("10");
    var secondNumber = parseInt("20");
    var c = firstNumber + secondNumber;//Output is 30}
         
Q7. What is the output of parseInt (“20.5”)+ parseInt (“10.3”)?
Output is 30, decimal part is ignored.

Q8. What is the use of parseFloat() function?
To retain the decimal places, use parseFloat() function.
function addNumbers()
{
    var firstNumber = parseFloat(“20.5”);
    var secondNumber = parseFloat(“10.3");
   var c= firstNumber + secondNumber;//Output is 30.8
}

Q9. What is NaN & isNaN() function in Javascript?
NaN in JavaScript stands for Not-a-Number. In JavaScript we have isNaN() function which determines whether a value is an illegal number. This function returns true if the value is not a number, and false if not.
function addNumbers()
{
    var firstNumber = parseFloat(document.getElementById("txtFirstNumber").value);
    if (isNaN(firstNumber))
    {
alert("Please enter a valid number in the first number textbox");
        return;
    }
    var secondNumber = parseFloat(document.getElementById("txtSecondNumber").value);
    if (isNaN(secondNumber))
    {
alert("Please enter a valid number in the second number textbox");
        return;
    }
    document.getElementById("txtResult").value = firstNumber + secondNumber;
}
Now, when you leave first number and second number textboxes blank or if you enter text instead of a number, and when you click the Add button, you get validation error messages as expected.

Q10. How to make the validation error message more relevant?
We are better understand the validation error message by below example.
function addNumbers()
{  var firstNumber = document.getElementById("txtOneValue").value;
    var secondNumber = document.getElementById("txtTwoValue").value;
    if (firstNumber == "")
    { alert("First Number is required");   return;  }

    firstNumber = parseFloat(firstNumber);
    if (isNaN(firstNumber))
    { alert("Please enter a valid number in the first textbox");  return; }

    if (secondNumber == "")
    { alert("Second Number is required");  return; }

    secondNumber = parseFloat(secondNumber)
    if (isNaN(secondNumber))
    { alert("Please enter a valid number in the second textbox");   return;  }
    document.getElementById("txtResult").value = firstNumber + secondNumber;
}

Q11. What is the output of below statements?
var string1="string in double quotes"  var string2 = 'string in single quotes'

Both string1 and string2 performing same output. You can use either single or double quotes in a single statement.

Q12. How to Concatenating strings in JavaScript?
There are 2 options either use + operator or concat() method.
using + operator:
var string1 = "Hello"  var string2 = "JavaScript"
var result = string1 + " " + string2;
alert(result); Output : Hello JavaScript

using concat() method:
var string1 = "Hello"  var string2 = "JavaScript"
var result = string1.concat(" ", string2);
alert(result);  Output : Hello JavaScript

Q13. How to use single quotes inside a string in Java script?
There are 2 options:
Option 1: Use single quotes inside the string wherever you need them.
Example : var myString = "Welcome to 'JavaScript' Training";
alert(myString); 
Output : Welcome to 'JavaScript' Training

Option 2: Use escape sequence character \ with a single quote inside the string.
Example : var myString = 'Welcome to \'JavaScript\' Training';
alert(myString);
Output : Welcome to 'JavaScript' Training

Q14. How to use double quotes inside a string in Java script?
There are 2 options:
Option 1: Use double quotes inside the string wherever you need them.
Example : var myString = ‘Welcome to “JavaScript” Training’;
alert(myString);  Output : Welcome to “JavaScript” Training

Option 2: Use escape sequence character \ with a single quote inside the string.
Example : var myString = “Welcome to \”JavaScript\” Training”;
alert(myString); Output : Welcome to “JavaScript” Training

Q15. How to Converting a string to uppercase in JavaScript?
Use toUpperCase() function:
Example : var upperCaseString = "JavaScript";
alert(upperCaseString.toUpperCase());
Output : JAVASCRIPT

Q16. How to Converting a string to lowercase in JavaScript? 
Use toLowerCase() function:
Example :var lowerCaseString = "JavaScript";
alert(lowerCaseString.toLowerCase());
Output : javascript

Q17. How to find Length of string in JavaScript?
Use length property:
Example : alert("JavaScript".length); 
Output : 10

Example :var myString = "Hello JavaScript";
alert(myString.length); 
Output : 16

Q18. How to remove whitespace from both ends of a string in JavaScript?
Use trim() function:
Example :
var string1 = " AB ";
var string2 = " CD ";
var result = string1.trim() + string2.trim();
alert(result); 
Output : ABCD

Q19. How to Replace strings in JavaScript?
Use replace() method:
This method searches a given string for a specified value or a regular expression, replaces the specified values with the replacement values and returns a new string. This method does not change the original string.
Example: Replaces JavaScript with World
var myString = "Hello JavaScript";
var result = myString.replace("JavaScript", "World");
alert(result);
Output : Hello World

Q20. How to perform a case-sensitive global replacement in JavaScript?
Using a regular expression between the 2 forward slashes (//). The letter g after the forward slash specifies a global replacement. The match here is case sensitive. This means Red(with capital R) is not replaced with green.

var myString = "A Red bottle with a red liquid is on a red table";
var result = myString.replace(/red/g, "green");
//g means global replacement with case sensitive value
alert(result); 
Output : A Red bottle with a green liquid is on a green table

Q21. How to Perform a case-insensitive global replacement in JavaScript?
The letters gi after the forward slash indicates to do global case-insensitive replacement. Notice that the word Red (with capital R) is also replaced with green.
var myString = "A Red bottle with a red liquid is on a red table";
var result = myString.replace(/red/gi, "green");
alert(result); 
Output : A green bottle with a green liquid is on a green table 

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 Frequently asked Javascript interview questions. Thank You!

If you find something new to learn today from frequently 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


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Commonly Asked Javascript Interview Questions For Front End Developer

 No comments   

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
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);

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;
}
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);
}

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.

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?
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()
{  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";
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";
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



Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Older Posts Home

Sample Text

Copyright © Football Tutorial | Powered by Blogger
Design by | Blogger