Javascript interview questions: Hi Guys, if you don’t know about Best Javascript interview
questions, and you are searching for Best
Javascript interview questions to learn. Than you are at the right place.
Latest Best
Javascript interview questions of 2020
The Best JavaScript interview questions with answers for Front End
Developer are given below.
Q1. What is function in java script?
A
JavaScript function is a block of code. This function perform a specific task.
This function executes
when "something" invokes it or calls it.
Ex:
function myFunc(a, b) {
return a + b; // The function returns the sum of a and b.
}
Q2. How
code executes in a function in java script?
The code executes, in a
function, is placed inside curly brackets: {}
function myFunc(param1,
param2, ……) {
// code to be executed
}
Note: Inside the function, parameters behave as local
variables.
Q3. Why
Functions in Java script?
Code reusability: Define
the code once, and use it many times.
You can use the same
code many times with different arguments, to produce different results.
Q4. When
we invoke function in JavaScript?
We
invoke function in JavaScript when:
An
event occurs (button click, mouse up, mouse down, etc.…)
- Called from a JavaScript code.
- Automatically/
self-invoked
Q5. What is return statement in a function
in java script?
When
function reaches the return statement, the function will stop executing.
At
this point functions compute a return value. The return value is returned back
to the caller.
Example: Calculate the
sum of two numbers, and return the result:
var myVal = sumFunction(4,
3); // sumFunction is called, return
value will end up in myVar.
function sumFunction (x,
y) {
return x +y; // Function returns the sum of x
and y
}
The result in x will be:
7
Q6. What
are objects in a JavaScript?
Objects are collection
of values. Each value can be written as name:
value pairs.
Ex: var
person = {firstName:"John", lastName:"Doe", age: 33};
Q7. What
are Properties in java script?
The name: values pairs
in JavaScript objects are called properties:
Property Property Value
firstName John
lastName Doe
age 33
Q8. How
to Accessing Object Properties in java script?
You can access object
properties in two ways:
objectName.propertyName or objectName["propertyName"]
Example1:
person.lastName;
Example2: person["lastName"];
Q9. What
is Object Methods in Java script?
A method is a function
stored as a property.
Example
var person = {
firstName:
"John",
lastName
: "Doe",
id : 5566,
fullName
: function() {
return this.firstName + " " + this.lastName;
}
};
Q10. What
is this Keyword in a function in Java script?
In a
function, this refers to the "owner" of the function.
In the
above example, this is the person object that "owns" the fullName
function.
Q11. How to access Object Methods in java script?
You can
access an object method by following syntax: objectname.methodname()
Example:
name = person.fullName();
Q12. What is events in Java Script?
An HTML
event is an activity done by the browser, or done by a user.
Ex:
Events
|
Description
|
onclick
|
occurs
when HTML element is clicked.
|
onfocus
|
occurs
when an HTML element gets focus.
|
onblur
|
occurs
when HTML form loses the focus from an element.
|
onsubmit
|
occurs
when HTML form is submitted.
|
Onmouseover
|
occurs
when mouse is moved over an HTML element.
|
onmouseout
|
occurs when mouse is moved out from an HTML
element.
|
onmousedown
|
occurs
when mouse button is pressed over an html element.
|
onmouseup
|
occurs
when mouse is released from an html element.
|
onload
|
occurs
when document, frameset is loaded.
|
onunload
|
occurs
when body or frameset is unloaded.
|
onscroll
|
occurs
when document is scrolled.
|
onresized
|
occurs
when document is resized.
|
onreset
|
occurs
when form is reset.
|
onkeydown
|
occurs
when key is being pressed.
|
onkeypress
|
occurs
when user presses the key.
|
onkeyup
|
occurs
when key is released.
|
Q13. How event handler attributes used in Java script?
We used
it in two ways:
With
single quotes: <element event='some JavaScript'>
Ex:
<button
onclick=’displayDate()’>The date is?</button>
With
double quotes:<element event="some JavaScript">
Ex:<button
onclick="displayDate()">The date is?</button>
Q14. What is JavaScript string?
A
JavaScript string is zero or more characters written inside single or double
quotes.
In JavaScript
strings are used for storing and manipulating text.
Ex:
var x =
‘John Doe’;
Ex:
var x =
"John Doe";
Q15. How
to retrieve a substring from a given string in Java
Script?
The following 3 methods
are used to retrieve a substring from a given string:
1. substring()
2. substr()
3. slice()
substring()
method : This method extracts the characters from a string. It always
returns a new substring. This method has 2 parameters start and end. The
character at the end position is not included in the substring.
Syntax:
string.substring(start, end)
start argument is
required.
end argument is
optional.
Ex:
var str = "Hello
India!";
Positions:
H(0 Position)
e(1st
position) and so on.
var res =
str.substring(6);//Start from 6th character.
Output: India
Ex: Extract the first 10
characters
var str =
"JavaScript World";
var result =
str.substring(0, 10);// 0 to 9th charachers
Output: JavaScript
Ex: If "start"
>"end", it will swap the two arguments:
var str =
"India!";
var res =
str.substring(5,1);// Here 5 is greater than 1
Output: ndia
substr()
method : This method
has 2 parameters start and count. Start argument is required. Count argument is
optional.
Extract the first 10
characters
var str =
"JavaScript World";
var result =
str.substr(0, 10);
Output : JavaScript
Ex:
var str =
"JavaScript World";
var result =
str.substr(11);
alert(result);
Output : World
slice()
method : This method extracts the characters from a string. It always
returns a new substring. This method has 2 parameters start and end. The
character at the end position is not included in the substring.
Syntax:
string.slice(start, end)
start argument is
required.
end argument is
optional.
Ex: Extract the first 10
characters
var str =
"JavaScript World";
var result =
str.slice(0, 10);
Output: JavaScript
Ex: If the end parameter
is not specified.
var str = "JavaScript
World";
var result = str.slice(11);
Output : World
Q16. What is the
difference between substr and substring methods
Main difference:
Main difference:
1. Second parameter of substring()
method specifies the index position where as second parameter of substr()
method specifies the number of characters to return.
2. substr() method does not work in
IE8 and earlier versions.
Ex: Extract the first 10
characters
var str =
"JavaScript World";
var result =
str.substring(0, 10);// 0 to 9th charachers
Output: JavaScript
Ex: Extract the first
10 characters
var str =
"JavaScript World";
var result =
str.substr(0, 10);
Output: JavaScript
Q17. What is the difference between slice
and substring?
If
start parameter is greater than stop parameter, then substring will swap those
2 parameters, whereas slice will not swap.
Q18. Where
we can use indexOf() and substring() methods?
function
getEmailandDomainParts()
{ var emailAddress = document.getElementById("txtEmailAddress").value;
var emailPart = emailAddress.substring(0,
emailAddress.indexOf("@"));
var domainPart =
emailAddress.substring(emailAddress.indexOf("@") + 1);
document.getElementById("txtEmailPart").value = emailPart;
document.getElementById("txtDomainPart").value = domainPart;
}
Input:
anmolg4u@gmail.com
Output:
anmolg4u (Email Part) & gmail.com (domain part).
lastIndexOf()
method returns the position of the last occurrence of a specified value in a
string. Since it's job is to return the last index of the specified value, this
method searches the given string from the end to the beginning and returns the
index of the first match it finds. This method returns -1 if the specified
value is not available in the given string.
Example: Retrieve the
last index position of dot (.) in the given string
var url =
"http://www.mytutorials.com";
alert(url.lastIndexOf("."));
Output : 22
Q19. Give
me real time example where lastIndexOf and substring methods can be used?
Ex: Find the domain
name from the url.
<script
type="text/javascript">
function getDomainName()
{
var url = document.getElementById("txtURL").value;
var domainName =
url.substr(url.lastIndexOf("."));
document.getElementById("txtDomian").value = domainName;
}
</script>
Final words
So Guys, This is my own Javascript interview
questions For Front
End Developer. I hope this article is helpful
for you, please do share and comment you’re thought about this all Best Javascript interview
questions. Thank You!
If you find something new to
learn today from best 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