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.
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()
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:
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";
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";
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);
Example : alert("JavaScript".length);
Output : 10
Example :var myString = "Hello JavaScript";
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 :
Example :
var string1 = "
AB ";
var string2 = "
CD ";
var result = string1.trim() + string2.trim();
alert(result);
Output : ABCD
Q19. How to Replace strings in JavaScript?
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
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?
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 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