0% found this document useful (0 votes)
0 views11 pages

JavaScript Interview Questions for Freshers

The document provides a comprehensive list of JavaScript interview questions for freshers and experienced candidates, covering topics such as data types, functions, error handling, and DOM manipulation. It includes explanations of key concepts like closures, variable typing, and the differences between JavaScript and other languages. Additionally, it highlights the advantages of JavaScript and its dynamic nature, making it a crucial language for web development.

Uploaded by

Sinchana M A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views11 pages

JavaScript Interview Questions for Freshers

The document provides a comprehensive list of JavaScript interview questions for freshers and experienced candidates, covering topics such as data types, functions, error handling, and DOM manipulation. It includes explanations of key concepts like closures, variable typing, and the differences between JavaScript and other languages. Additionally, it highlights the advantages of JavaScript and its dynamic nature, making it a crucial language for web development.

Uploaded by

Sinchana M A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JavaScript Interview Questions for Freshers

Let’s discuss some common questions that you should prepare for the
interviews. These questions will be helpful in clearing the interviews specially for
the frontend development role.
1. What are the differences between Java and JavaScript?
JavaScript is a client-side scripting language and Java is object Oriented
Programming language. Both of them are totally different from each other.
 JavaScript: It is a light-weighted programming language (“scripting
language”) for developing interactive web pages. It can insert dynamic
text into the HTML elements. JavaScript is also known as the browser’s
language.
 Java: Java is one of the most popular programming languages. It is an
object-oriented programming language and has a virtual machine
platform that allows you to create compiled programs that run on nearly
every platform. Java promised, “Write Once, Run Anywhere”.
2. What are JavaScript Data Types?
There are three major Data types in JavaScript.
 Primitive
 Numbers
 Strings
 Boolean
 Symbol
 Trivial
 Undefined
 Null
 Composite
 Objects
 Functions
 Arrays
3. Which symbol is used for comments in JavaScript?
Comments prevent the execution of statements. Comments are ignored while
the compiler executes the code. There are two type of symbols to represent
comments in JavaScript:
 Double slash: It is known as a single-line comment.
// Single line comment

 Slash with Asterisk: It is known as a multi-line comment.


/*
Multi-line comments
...
*/

4. What would be the result of 3+2+”7″?


Here, 3 and 2 behave like an integer, and “7” behaves like a string. So 3 plus 2
will be 5. Then the output will be 5+”7″ = 57.
5. What is the use of the isNaN function?
The number isNan function determines whether the passed value is NaN (Not a
number) and is of the type “Number”. In JavaScript, the value NaN is considered
a type of number. It returns true if the argument is not a number, else it returns
false.
6. Which is faster in JavaScript and ASP script?
JavaScript is faster compared to ASP Script. JavaScript is a client-side scripting
language and does not depend on the server to execute. The ASP script is a
server-side scripting language always dependable on the server.
7. What is negative infinity?
The negative infinity is a constant value represents the lowest available value. It
means that no other number is lesser than this value. It can be generate using a
self-made function or by an arithmetic operation. JavaScript shows the
NEGATIVE_INFINITY value as -Infinity.
8. Is it possible to break JavaScript Code into several lines?
Yes, it is possible to break the JavaScript code into several lines in a string
statement. It can be broken by using the backslash ‘\’.
For example:
document.write("A Online Computer Science Portal\ for Geeks")
The code-breaking line is avoid by JavaScript which is not preferable.
let gfg= 10, GFG = 5,
Geeks =
gfg + GFG;
9. Which company developed JavaScript?
Netscape developed JavaScript and was created by Brenden Eich in the year of
1995.
10. What are undeclared and undefined variables?
 Undefined: It occurs when a variable is declare not not assign any
value. Undefined is not a keyword.
 Undeclared: It occurs when we try to access any variable which is not
initialize or declare earlier using the var or const keyword. If we
use ‘typeof’ operator to get the value of an undeclare variable, we will
face the runtime error with the return value as “undefined”. The scope
of the undeclare variables is always global.
11. Write a JavaScript code for adding new elements dynamically.
 html

<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>

<body>
<button onclick="create()">
Click Here!
</button>

<script>
function create() {
let geeks = document.createElement('geeks');
geeks.textContent = "Geeksforgeeks";
geeks.setAttribute('class', 'note');
document.body.appendChild(geeks);
}
</script>
</body>
</html>

12. What are global variables? How are these variables declared,
and what are the problems associated with them?
In contrast, global variables are the variables that define outside of functions.
These variables have a global scope, so they can be used by any function
without passing them to the function as parameters.
Example:
 javascript

let petName = "Rocky"; //Global Variable


myFunction();

function myFunction() {
document.getElementById("geeks").innerHTML
= typeof petName + "- " +
"My pet name is " + petName;
}

document.getElementById("Geeks")
.innerHTML = typeof petName + "- " +
"My pet name is " + petName;

It is difficult to debug and test the code that relies on global variables.
13. What do you mean by NULL in JavaScript?
The NULL value represents that no value or no object. It is known as empty
value/object.
14. How to delete property-specific values?
The delete keyword deletes the whole property and all the values at once like
let gfg={Course: "DSA", Duration:30};
delete gfg.Course;
15. What is a prompt box?
The prompt box is a dialog box with an optional message prompting the user to
input some text. It is often used if the user wants to input a value before
entering a page. It returns a string containing the text entered by the user, or
null.
16. What is the ‘this’ keyword in JavaScript?
Functions in JavaScript are essential objects. Like objects, it can be assign to
variables, pass to other functions, and return from functions. And much like
objects, they have their own properties. ‘this’ stores the current execution
context of the JavaScript program. Thus, when it use inside a function, the value
of ‘this’ will change depending on how the function is defined, how it is invoked,
and the default execution context.
17. Explain the working of timers in JavaScript. Also elucidate the
drawbacks of using the timer, if any.
The timer executes some specific code at a specific time or any small amount of
code in repetition to do that you need to use the
functions setTimout, setInterval, and clearInterval. If the JavaScript code
sets the timer to 2 minutes and when the times are up then the page displays an
alert message “times up”. The setTimeout() method calls a function or
evaluates an expression after a specified number of milliseconds.
18. What is the difference between ViewState and SessionState?
 ViewState: It is specific to a single page in a session.
 SessionState: It is user specific that can access all the data on the web
pages.
19. How to submit a form using JavaScript?
You can use document.form[0].submit() method to submit the form in
JavaScript.
20. Does JavaScript support automatic type conversion?
Yes, JavaScript supports automatic type conversion.
JavaScript Intermediate Interview Questions
21. What are all the looping structures in JavaScript ?
 while loop: A while loop is a control flow statement that allows code to
be executed repeatedly based on a given Boolean condition. The while
loop can be thought of as a repeating if statement.
 for loop: A for loop provides a concise way of writing the loop structure.
Unlike a while loop, for statement consumes the initialization, condition
and increment/decrement in one line thereby providing a shorter, easy
to debug structure of looping.
 do while: A do-while loop is similar to while loop with the only
difference that it checks the condition after executing the statements,
and therefore is an example of Exit Control Loop.
22. How can the style/class of an element be changed?
To change the style/class of an element there are two possible ways. We
use document.getElementByID method
document.getElementById("myText").style.fontSize = "16px;
document.getElementById("myText").className = "class";
23. Explain how to read and write a file using JavaScript?
 The readFile() functions is used for reading operation.
readFile( Path, Options, Callback)
 The writeFile() functions is used for writing operation.
writeFile( Path, Data, Callback)
24. What is called Variable typing in JavaScript ?
The variable typing is the type of variable used to store a number and using
that same variable to assign a “string”.
Geeks = 42;
Geeks = "GeeksforGeeks";
25. How to convert the string of any base to integer in JavaScript?
In JavaScript, parseInt() function is used to convert the string to an integer. This
function returns an integer of base which is specified in second argument of
parseInt() function. The parseInt() function returns Nan (not a number) when the
string doesn’t contain number.
26. Explain how to detect the operating system on the client
machine?
To detect the operating system on the client machine, one can simply use
navigator.appVersion or navigator.userAgent property. The Navigator appVersion
property is a read-only property and it returns the string that represents the
version information of the browser.
27. What are the types of Pop up boxes available in JavaScript?
There are three types of pop boxes available in JavaScript.
 Alert
 Confirm
 Prompt
28. What is the difference between an alert box and a confirmation
box?
An alert box will display only one button which is the OK button. It is used to
inform the user about the agreement has to agree. But a Confirmation box
displays two buttons OK and cancel, where the user can decide to agree or not.
29. What is the disadvantage of using innerHTML in JavaScript?
There are lots of disadvantages of using the innerHTML in JavaScript as the
content will replace everywhere. If you use += like “innerHTML = innerHTML +
‘html’” still the old content is replaced by HTML. It preserves event handlers
attached to any DOM elements.
30. What is the use of void(0) ?
The void(0) is used to call another method without refreshing the page during
the calling time parameter “zero” will be passed.
For further reading, check out our dedicated article on Intermediate
Javascript Interview Questions. Inside, you’ll discover over 20 questions with
detailed answers.

JavaScript Interview Questions for Experienced


31. What is the ‘Strict’ mode in JavaScript and how can it be
enabled?
Strict Mode is a new feature in ECMAScript 5 that allows you to place a program
or a function in a “strict” operating context. This strict context prevents certain
actions from being taken and throws more exceptions. The statement “use
strict” instructs the browser to use the Strict mode, which is a reduced and safer
feature set of JavaScript.
32. How to get the status of a CheckBox?
The DOM Input Checkbox Property is used to set or return the checked status of
a checkbox field. This property is used to reflect the HTML Checked attribute.
document.getElementById("GFG").checked;
If the CheckBox is checked then it returns True.
33. How to explain closures in JavaScript and when to use it?
The closure is created when a child functions to keep the environment of the
parent’s scope even after the parent’s function has already executed. The
Closure is a locally declared variable related to a function. The closure will
provide better control over the code when using them.
 Javascript

// Explanation of closure
function foo() {
let b = 1;
function inner() {
return b;
}
return inner;
}
let get_func_inner = foo();

console.log(get_func_inner());
console.log(get_func_inner());
console.log(get_func_inner());

34. What is the difference between call() and apply() methods ?


Both methods are used in a different situation
 call() Method: It calls the method, taking the owner object as
argument. The keyword this refers to the ‘owner’ of the function or the
object it belongs to. We can call a method that can be used on different
objects.
 apply() Method: The apply() method is used to write methods, which
can be used on different objects. It is different from the function call()
because it takes arguments as an array.
35. How to target a particular frame from a hyperlink in
JavaScript ?
This can be done by using the target attribute in the hyperlink. Like
<a href="/geeksforgeeks.htm" target="newframe">New Page</a>
36. Write the errors shown in JavaScript?
There are three different types of errors in JavaScript.
 Syntax error: A syntax error is an error in the syntax of a sequence of
characters or tokens that are intended to be written in a particular
programming language.
 Logical error: It is the most difficult error to be traced as it is the error
on the logical part of the coding or logical error is a bug in a program
that causes to operate incorrectly and terminate abnormally.
 Runtime Error: A runtime error is an error that occurs during the
running of the program, also known as an exception.
37. What is the difference between JavaScript and Jscript?
JavaScript
 It is a scripting language developed by Netscape.
 It is used to design client and server-side applications.
 It is completely independent of Java language.
Jscript
 It is a scripting language developed by Microsoft.
 It is used to design active online content for the word wide Web.
38. What does var myArray = [[]]; statement declares?
In JavaScript, this statement is used to declare a two-dimensional array.
39. How many ways an HTML element can be accessed in
JavaScript code?
There are four possible ways to access HTML elements in JavaScript which are:
 getElementById() Method: It is used to get the element by its id
name.
 getElementsByClass() Method: It is used to get all the elements that
have the given classname.
 getElementsByTagName() Method: It is used to get all the elements
that have the given tag name.
 querySelector() Method: This function takes CSS style selector and
returns the first selected element.
40. What is the difference between innerHTML & innerText?
The innerText property sets or returns the text content as plain text of the
specified node, and all its descendants whereas the innerHTML property sets or
returns the plain text or HTML contents in the elements. Unlike innerText, inner
HTML lets you work with HTML rich text and doesn’t automatically encode and
decode text.
41. What is an event bubbling in JavaScript?
Consider a situation an element is present inside another element and both of
them handle an event. When an event occurs in bubbling, the innermost element
handles the event first, then the outer, and so on.

20. Explain Closures in JavaScript.

Closures are an ability of a function to remember the variables and functions that are
declared in its outer scope.

9. Explain passed by value and passed by reference.

In JavaScript, primitive data types are passed by value and non-primitive data
types are passed by reference.
7. Is javascript a statically typed or a dynamically typed language?

JavaScript is a dynamically typed language. In a dynamically typed language, the type


of a variable is checked during run-time in contrast to a statically typed language,
where the type of a variable is checked during compile-time.

21. Mention some advantages of javascript.

There are many advantages of javascript. Some of them are

1. Javascript is executed on the client-side as well as server-side also. There are a


variety of Frontend Frameworks that you may study and utilize. However, if you
want to use JavaScript on the backend, you'll need to learn NodeJS. It is currently
the only JavaScript framework that may be used on the backend.

2. Javascript is a simple language to learn.

3. Web pages now have more functionality because of Javascript.

4. To the end-user, Javascript is quite quick.

23. What are callbacks?


A callback is a function that will be executed after another function gets
executed. In javascript, functions are treated as first-class citizens, they can be
used as an argument of another function, can be returned by another function,
and can be used as a property of an object

26. What is recursion in a programming language?

Recursion is a technique to iterate over an operation by having a function call itself


repeatedly until it arrives at a result.

27. What is the use of a constructor function in javascript?

Constructor functions are used to create objects in javascript.

28. What is DOM?

 DOM stands for Document Object Model. DOM is a programming interface for
HTML and XML documents.

29. Which method is used to retrieve a character from a certain


index?

The charAt() function of the JavaScript string finds a char element at the supplied
index.

30. What do you mean by BOM?

Browser Object Model is known as BOM. It allows users to interact with the browser. A
browser's initial object is a window. As a result, you may call all of the window's
functions directly or by referencing the window. The document, history, screen,
navigator, location, and other attributes are available in the window object

2) List some features of JavaScript.


Some of the features of JavaScript are:

o Lightweight

o Interpreted programming language

o Good for the applications which are network-centric

o Complementary to Java

o Complementary to HTML
o Open source

Cross-platform

5) List some of the disadvantages of JavaScript.


Some of the disadvantages of JavaScript are:

o No support for multithreading

o No support for multiprocessing

o Reading and writing of files is not allowed

o No support for networking applications.

28) How to create objects in JavaScript?


There are 3 ways to create an object in JavaScript.

1. By object literal

2. By creating an instance of Object

3. By Object Constructor

Let's see a simple code to create an object using object literal.

1. emp={id:102,name:"Rahul Kumar",salary:50000}
More details.

29) How to create an array in JavaScript?


There are 3 ways to create an array in JavaScript.

1. By array literal

2. By creating an instance of Array

3. By using an Array constructor

Let's see a simple code to create an array using object literal.


1. var emp=["Shyam","Vimal","Ratan"];

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy