Answer:: What Is Scope Variable in Javascript?
Answer:: What Is Scope Variable in Javascript?
Answer:: What Is Scope Variable in Javascript?
Answer :
The scope is set of objects, variables and function and the JavaScript can have global scope variable and local
scope variable.
The global scope is a window object and its used out of function and within the functions.
The local scope is a function object and its used within the functions.
In the JavaScript, when we write more than one functions with same name that time JavaScript consider the last
define function and override the previous functions
Answer :
Following are different data-type in JavaScript.
1.String
2.Number
3.Boolean
4.Function
5.Object
6.Null
7.Undefined
Question 25. The Terms “scope” And “context” Refer To The Same Thing In Javascript ?
Answer :
False, Scope pertains to the visibility of variables, and context refers to the object to which a method belongs (which
can be changed by using call or apply)
Question 27. How Do You Determine If A Javascript Instance Object Was Created From A Specific Constructor Or
Not?
Answer :
Use the instanceof operato
Question 29. When You Create A Function Inside Of A Method, What Does The “this” Keyword Refer To When Used
In That Function?
Answer :
The “window” object, Inside of a function, the “this” keyword always refers to the window object. But when a funciton
is a property of on object, the “this” keyword always refers to the object that the function is a method of.
Question 31. Can A Javascript Constructor Return A Primitive Value (e.g. A Number Or A String)?
Answer :
No. A JavaScript constructor can only return an object. When no return value is specified, it returns an instance of
itself. If an object is specified as the return value, then that object is the return value. If any value other than an
object is specified as the return value, then it returns an instance of itself.
Question 33. What Is The Difference Between A Constructor Function And A Non-constructor Function With
Respect To The Word “this” ?
Answer :
In a non-constructor function, “this” refers to the global context or if the function is a method, it refers to the object
to which the method belongs. In the instance object that is returned by a constructor function, “this” refers to the
instance object itself.
Question 37. What Is The Difference Between Using Dot Notation And Bracket Notation When Accessing An Object’s
Property?
Answer :
If using dot notation, the property must be a string and refer to a property that exists.
When using bracket notation, any valid JavaScript expression that produces a value can be used inside the
brackets, such as a variable or an an array element.
Scope pertains to the visibility of variables and in contrast, context refers to the object to which a method
belongs (which can be changed by using call or apply).
Q10. Can you assign an anonymous function to a variable and pass it as an argument to another
function?
Yes! An anonymous function can be assigned to a variable. It can also be passed as an argument to another
function
Q13. What is the purpose of ‘This’ operator in JavaScript?
The JavaScript this keyword refers to the object it belongs to. This has different values depending on where it is
used. In a method, this refers to the owner object and in a function, this refers to the global object.
Q14. What is Callback?
A callback is a plain JavaScript function passed to some method as an argument or option. It is a function that
is to be executed after another function has finished executing, hence the name ‘call back‘. In JavaScript,
functions are objects. Because of this, functions can take functions as arguments, and can be returned by other
functions.
some inner scope. It gives you access to an outer function’s scope from an inner function. In JavaScript, closures
are created every time a function is created. To use a closure, simply define a function inside another function
and expose it
The simplest way to create a cookie is to assign a string value to the document.cookie object, which looks like
this-
Syntax :
So you can use this string whenever you want to access the cookie.
•The document.cookie string will keep a list of name = value pairs separated by semicolons, where
name is the name of a cookie and value is its string value.
•You can use strings’ split() function to break the string into key and values.
Property- is the value assigned to the property like type=”text”, value=’Name’ etc.
Q23. List out the different ways an HTML element can be accessed in a JavaScript code.
Here are the list of ways an HTML element can be accessed in a Javascript code:
(iii) getElementsByTagName(‘tagname’): Gets all the elements that have the given tag name.
(iv) querySelector(): This function takes css style selector and returns the first selected element.
Typed Language is in which the values are associated with values and not with variables. It is of two types:
•Dynamically: in this, the variable can hold multiple types; like in JS a variable can take number, chars.
•Statically: in this, the variable can hold only one type, like in Java a variable declared of string can take
only set of characters and nothing else.
Local Storage – The data is not sent back to the server for every HTTP request (HTML, images, JavaScript, CSS,
etc) – reducing the amount of traffic between client and server. It will stay until it is manually cleared through
settings or program.
Session Storage – It is similar to local storage; the only difference is while data stored in local storage has no
expiration time, data stored in session storage gets cleared when the page session ends. Session Storage will
In case you are facing any challenges with these JavaScript Interview Questions, please comment on your
is an assignment value. It can be assigned to a variable as a representation of no value. Also, undefined and null
are two distinct types: undefined is a type itself (undefined) while null is an object.
Undeclared variables are those that do not exist in a program and are not declared. If the program tries to
read the value of an undeclared variable, then a runtime error is encountered. Undefined variables are those
that are declared in the program but have not been given any value. If the program tries to read the value of an
Window Document
JavaScript window is a global object which The document also comes under the window and can
holds variables, functions, history, location. be considered as the property of the window.
Event bubbling is a way of event propagation in the HTML DOM API, when an event occurs in an element
inside another element, and both elements have registered a handle for that event. With bubbling, the event is
first captured and handled by the innermost element and then propagated to outer elements. The execution
starts from that event and goes to its parent element. Then the execution passes to its parent element and so
Q37. How can you convert the string of any base to integer in JavaScript?
The parseInt() function is used to convert numbers between different bases. It takes the string to be converted
as its first parameter, and the second parameter is the base of the given string.
For example-
1parseInt("4F", 16)
Q39. What are Exports & Imports?
Imports and exports help us to write modular JavaScript code. Using Imports and exports we can split our code
•When you use strict mode, you cannot use implicitly declared variables, or assign a value to a read-only
property, or add a property to an object that is not extensible.
•You can enable strict mode by adding “use strict” at the beginning of a file, a program, or a function.
The call() method calls a function with a given this value and arguments provided individually.
Syntax-
The apply() method calls a function with a given this value, and arguments provided as an array.
Syntax-
1fun.apply(thisArg, [argsArray])
OOPs refers to Object-Oriented Programming. It is the programming paradigm that is defined using objects. Objects can be
considered as real-world instances of entities like class, that have some characteristics and behaviors
OOPs or Object Oriented Programming mainly comprises of the below four features, and make sure
you don't miss any of these:
Inheritance
Encapsulation
Polymorphism
Data Abstraction
7. What are some advantages of using OOPs?
A class can be understood as a template or a blueprint, which contains some values, known as member
data or member, and some set of rules, known as behaviors or functions. So when an object is created,
it automatically takes the data and functions that are defined in the class.
Therefore the class is basically a template or blueprint for objects. Also one can create as many objects
as they want based on a class.
For example, first, a car’s template is created. Then multiple units of car are created based on that
template.
10. What is an object?
An object refers to the instance of the class, which contains the instance of the members and behaviors
defined in the class template. In the real world, an object is an actual entity to which a user interacts,
whereas class is just the blueprint for that object. So the objects consume space and have some
characteristic behavior.
For example, a specific car
One can visualize Encapsulation as the method of putting everything that is required to do the job, inside a capsule and
presenting that capsule to the user. What it means is that by Encapsulation, all the necessary data and methods are bind together
and all the unnecessary details are hidden to the normal user. So Encapsulation is the process of binding data members and
methods of a program together to do a specific job, without revealing unnecessary details.
1) Data hiding: Encapsulation is the process of hiding unwanted information, such as restricting access to any member of an
object.
2) Data binding: Encapsulation is the process of binding the data members and the methods together as a whole, as a class
The term “inheritance” means “receiving some quality or behavior from a parent to an offspring.” In
object-oriented programming, inheritance is the mechanism by which an object or class (referred to as a
child) is created using the definition of another object or class (referred to as a parent). Inheritance not
only helps to keep the implementation simpler but also helps to facilitate code reuse
The ‘this’ keyword in JavaScript refers to the currently calling object. It is commonly used in constructors to assign
values to object properties.
A callback is a JavaScript function that is passed to another function as an argument or a parameter. This function is
A cookie is generally a small data that is sent from a website and stored on the user’s machine by a web browser that
was used to access the website. Cookies are used to remember information for later use and also to record the
Arrow functions are a short and concise way of writing functions in JavaScript. The general syntax of an arrow
function is as below:
console.log("hello world!");
};
23. What are the different ways an HTML element can be accessed in a
JavaScript code?
Here are the ways an HTML element can be accessed in a JavaScript code:
getElementByClass(‘classname’): Gets all the HTML elements that have the specified classname.
getElementbyTagName(‘tagname’): Gets all the HTML elements that have the specified tagname.
querySelector(): Takes CSS style selector and returns the first selected HTML element.
32. What is the difference between Event Capturing and Event Bubbling?
This process starts with capturing the event of the This process starts with capturing the event of the
outermost element and then propagating it to the innermost element and then propagating it to the
innermost element. outermost element.
Call Apply
In the call() method, arguments are provided In the apply() method, arguments are provided in the form
individually along with a ‘this’ value. of an array along with a ‘this’ value.
There are two ways in which we can remove duplicates from a JavaScript array:
By Using the Filter Method
To call the filter() method, three arguments are required. These are namely array, current element, and index of the
current element.
•ceil() – returns the smallest integer that is greater than or equal to the given number
•constructor() – Returns the function that created the corresponding instance of the object
•Date.parse() – Parses a string representation of a date and time and then returns the internal millisecond representation
for the same
•filter() – Creates a new array with all the elements of the array for which the filtering function returns true
•localeCompare() – Returns a number that indicates whether a reference string comes before, after, or is the same as
the given string in the sort order
•reduce() – Applies a function simultaneously for two values of the array to reduce them to a single value
•round() – Rounds off the value of the given number to the nearest integer and returns the same
•slice() – Extracts a certain section of a string and returns the remaining string
•some() – returns true if at least one element of the array satisfies the provided testing function
•toLocaleString() – Return a string value of the current number in a format that depends on the browser’s locale
settings
Answer: All modern browsers (Mozilla Firefox, Safari, Google Chrome, etc.) come with an inbuilt debugger that can be
summoned by pressing the F12 key. You need to choose the Console tab to view the result. Here you can set breakpoints as well
as view the value of the variables.
JavaScript also features a debugger keyword that replicates the function of using breakpoints using a debugger. However, it
works only when the debugging option is enabled in the web browser settings.
Question: What are the different types of Error Name values in JavaScript?
Answer: There are 6 types of Error Name values. Each one of them is briefly explained as follows:
•Eval Error – Thrown when coming across an error in eval() (Newer JS releases don’t have it)
•Range Error – Generated when a number outside the specified range is used
•Syntax Error – When the incorrect syntax is used, we get this error
•Type Error – This error is thrown when a value outside the range of data types is tried to be used.
Answer: During page loading, the HTML code's parsing is paused by default until the script hasn’t stopped executing. This
results in a delay in displaying the web page if the server is slow or the script that is to be loaded is bulky.
Using deferred scripts results in a delay in the script's execution when the HTML parser is running. Hence, this results in a
reduction in the loading time of the webpage.
Q.2. What are the difference between a class component and functional component?
Ans.
✅Class Components
Class-based Components uses ES6 class syntax. It can make use of the lifecycle methods.
Class components extend from React.Component.
In here you have to use this keyword to access the props and functions that you declare inside
the class components.
✅Functional Components
Functional Components are simpler comparing to class-based functions.
Functional Components mainly focuses on the UI of the application, not on the behavior.
To be more precise these are basically render function in the class component.
Functional Components can have state and mimic lifecycle events using Reach Hooks
Q.3. What is difference between controlled vs uncontrolled component?
Ans.
✅Controlled Components
In HTML, form elements such as <input />, <textarea />, and <select /> typically maintain their own state and
update it based on user input. When a user submits a form, the values from the elements mentioned
above are sent with the form. With React it works differently. The component containing the form will
keep track of the value of the input in its state and will re-render the component each time the callback
function, e.g., onChange is fired as the state will be updated. An input form element whose value is
controlled by React in this way is called a "controlled component". You could also call this a "dumb
component".
✅Uncontrolled Components
A Uncontrolled Component is one that stores its own state internally, and you query the DOM using
a ref to find its current value when you need it. This is a bit more like traditional HTML.
Example
// Controlled:
<input type="text" value={value} onChange={handleChange} />
// Uncontrolled:
<input type="text" defaultValue="foo" ref={inputRef} />
// Use `inputRef.current.value` to read the current value of <input>
Q.5. What is prop drilling and how can you avoid it?
Ans. While passing a prop from each component to the next in the hierarchy from the source
component to the deeply nested component. This is called prop drilling.
To avoid prop drilling, a common approach is to use React context. This allows a Provider component
that supplies data to be defined, and allows nested components to consume context data via either
a Consumer component or a useContext hook.
Q4:
What are Higher-Order components?
Junior
React 155
Answer
A higher-order component (HOC) is a function that takes a component and returns a new component. Basically, it’s a pattern that is
derived from React’s compositional nature We call them as “pure’ components” because they can accept any dynamically provided
child component but they won’t modify or copy any behavior from their input components.
4. Props manipulation
Q7:
What does it mean for a component to be mounted in React?
Junior
React 155
Answer
Q8:
What is the difference between state and props?
Junior
React 155
Answer
Both props and state are plain JavaScript objects. While both of them hold information that influences the output of render, they are
different in their functionality with respect to component. i.e,
• State is managed within the component similar to variables declared within a function.
Q10:
What's the difference between a Controlled component and an Uncontrolled one in React?
Junior
React 155
Answer
This relates to stateful DOM components (form elements) and the React docs explain the difference:
• A Controlled Component is one that takes its current value through props and notifies changes through callbacks like onChange.
A parent component "controls" it by handling the callback and managing its own state and passing the new values as props
to the controlled component. You could also call this a "dumb component".
• A Uncontrolled Component is one that stores its own state internally, and you query the DOM using a ref to find its current
value when you need it. This is a bit more like traditional HTML.
Most native React form components support both controlled and uncontrolled usage:
Q13:
React Hooks: Does React useState Hook update immediately?
Mid
React 155
Problem
React useState and setState don’t make changes directly to the state object; they create queues to optimize performance, which is why the
changes don’t update immediately. The process to update React state is asynchronous for performance reasons.
To perform side effects after state has change, you must use the useEffect