61 LWC Lightning Web Component Interview Questions and Answers
61 LWC Lightning Web Component Interview Questions and Answers
Contact Us
Table of Contents
wire function
Whatsapp Chat
Lifecycle hooks
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 1/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
event.StopPropogation() Vs Event.preventDefault()?
Promise in async transactions?
What are web components, Is LWC based on web components?
Callback hell?
Can I deploy component with empty css file?
What is difference between var and let in JS?
Scenario based
For Beginners
For Experienced
Ans: Function also needs to have cacheable = true annotation ie should be like
@AuraEnabled(cacheable = true)
Whatsapp Chat
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 2/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Ans: First of all when you mark function as cacheable = true it can only retrieve data i.e. it
cant have any DML.
It is used to improve component performance by quickly showing cached data from client
side storage without waiting for server trip.
Remember to refresh the stale data provisioned by Apex we have to use refreshApex as
LDS doesn’t manage data coming from Apex ( In case of wired service)
Read more: Salesforce Admin Tutorial
3. Will the below code deploy? (Assuming all variables and apex
class function exists).
@wire(getBoats,{
boatTypeId : this.boatTypeId
})
getBoats(error,data){
}
Ans: No it wont when passing a variable in wire you should always use $ along with
variable, it should be written like
@wire(getBoats,{boatTypeId : ‘$boatTypeId})
Ans: $ prefix tells the wire service to treat values passed as a property of the class and
evaluate it as this.propertyName and the property is reactive. If the property’s value
changes, new data is provisioned and the component renders.
Join our Salesforce online course for comprehensive, hands-on training with real-time
projects. Benefit from expert guidance, daily notes, and interview preparation to excel in
your Salesforce career.
See the below code and answer the following question: [Scenario based Salesforce
Lightning Interview question with relevant answer]
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 3/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
@wire(getBoats,{boatTypeId : ‘$boatTypeId’})
getBoats({error,data}){
if(data){
this.someVar = data;
this.error = undefined;
}
else if(error){
this.error = error;
this.someVar = undefined ;
}
}
Ans:
@wire(getBoats,{boatTypeId : ‘$boatTypeId’})
getBoats(result){
this.mainResult = result;
if(result.data){
this.someVar = result.data;
this.error = undefined;
}
else if(result.error){
this.error = result.error;
this.someVar = undefined ;
}
}
searchBoats(event){
@wire(getBoats,{
boatTypeId: ‘$boatTypeId’
}
getBoats(data,error){
} Whatsapp Chat
}
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 4/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Assume searchBoats is being called on click of button? Will I be able to deploy the
code ?
Ans: No you cant call it like this , code will not deploy. You will receive error as leading
decorators must be attached to class which means decorators like wire can be directly
under class only not inside any other function.
Similarly if you try to define variable with @track or api decorator inside a function it will
fail.
Join 100% hands-on career building Salesforce online training. Enroll today for free
demo!
Ans: Wired service is called right after component is created ie after constructor and is
again called when parameter that you are passing is made available.
Connectedcallback : Called when the element is inserted into a document. This hook
flows from parent to child.
RenderedCallback : Called after every render of the component. This lifecycle hook is
specific to Lightning Web Components, it isn’t from the HTML custom elements
specification. This hook flows from child to parent. Ie its not part of HTMLElement rather
defined in LightningElement.
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 5/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Ans: True
10. What is the difference in below two codes , will they both
compile ? Give same results ?
Code 1 :
@wire(getBoats)
getBoats({data,error}){
if(data)
console.log(‘print here’);
Else if(error)
console.log(‘print in else’);
}
@wire(getBoats,{})
getBoats({error,data}){
if(data)
console.log(‘print here’);
Else if(error)
console.log(‘print in else’);
}
@wire(getBoats)
getBoats({myData,myError}){
if(mydata)
console.log(‘i am in data’); Whatsapp Chat
else if(myError)
Call us on:console.log(‘I
IN +91 93478 54179 am in 64857
, 93467 error’);
, USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 6/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
}
Will the code deploy successfully or I will receive an error ?
Ans: We cant use any other variable name other than data, error they are hardcoded in
wire service. Code will successfully deploy but wire service wont be able to fetch any data.
Ans: stopPropagation prevents further propagation of the current event in the capturing
and bubbling phases. preventDefault prevents the default action the browser makes on
that event.
10.
Ans: No unless you also add logic in backend javascript file to actually throw an error
using checkValidity and reportValidity.
Whatsapp Chat
14. Are quick actions supported for LWC components ?
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 7/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Ans: Quick actions are supported by LWC in Summer 21 or later orgs. LWC quick actions
are only supported on record pages.
Learn Salesforce programming with Step-by-Step Salesforce Apex Developer Tutorial
for beginners lessons.
Join 100% hands-on career building Salesforce online training. Enroll today for free
demo!
Ans:
Promise is object returned in async transaction which notifies you about completion or
failure of transaction.
For example when you call apex imperatively it returns you a promise object on the basis
of object returned execution either goes into (then) ie transaction was successful or
(catch) which means transaction failed.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 8/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Ans: In simplest language , web components can be explained as it allows you to create
and re-use custom components as html tags in a component with their functionality
encapsulated from rest of your code.
Web components has 4 pillars which make it so amazing.
HTML Template : user defined templates which are rendered until called upon.
Custom Elements : With this we can embed as components merely as html tags in our
template.
Shadow DOM : With this we can separate DOM of each custom element from each other
to make sure nothing from any components leaks into each other.
HTML Imports : You can import other html documents in another html document for
example we can import multiple templates into one component then use render function to
display a template.
Yes LWC is based on web components
If you look at LWC architecture Salesforce only adds security , LDS and base components
rest is based on web components and es 7.
Ans: LightningElement is custom wrapper on HTMLElement which actually contains all the
lifecycle hooks methods over which Salesforce did some customization.
Ans: Because of export default component only we are able to embed one component into
another.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 9/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Ans: We should never use “id” in lwc as id that you will define in LWC may get
transformed into something entirely different on rendering, rather use data-id in your tags
to reference them.
Ans: For parent to child communication you just need to expose your function or attribute
as @api then in your parent component you can use querySelector to actually query the
child component access exposed attribute or method.
Ans: We should fire up event from child component B to parent A then from A call attribute
/ function exposed (as @api) and pass data into C component.
Ans: These type of events can bubble up inside dom and also able to cross shadow
boundary.
Ans: No
Ans: No
Whatsapp
Ans: Callback functions are nothing but functions passed as parameter into other Chat
functions.
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 10/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Ans: Functions are neither async or sync in themselves rather depend on where are they
being passed for example if a function in passed into reduce method is sync but if function
is being passed into setTimeout function its async.
Ans: In most simple words callback hell occurs when there are many functions you want
to call async and you end up puting them one side each another and as the code grows it
becomes very difficult to read for example :
getData(function(x) {
getMoreData(x, function(y) {
getMoreData(y, function(z) { ... });
});
});
Ans: The Lightning Web Components programming model has three decorators that add
functionality to a property or function. There are 3 decorators for LWC
@track , @wire, @api
Ans: After Spring 20 all the properties are made by default reactive ie we dont need
@track for primitive properties. We still need it for array or object type properties
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 11/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Ans: In simple words difference is var is function scoped while let is block scoped
Var allows you to redeclare same variable while let will throw an error
Var variables are hoisted that is you can access them even before they are declared in
code its just they will return undefined value while with let it will throw an error.
Ans: We don’t have application event as such in LWC like Aura rather we have LMS in
LWC to communicate between components which are not part of same hierarchy.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 12/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Ans: Using navigation mixin only you can also create new record where you define object
, action as new and pass any default values you want to set.
Ans: Render is not part of lifecycle hook its a protected function, we only use it if we have
imported multiple templates in our component and we want to render particular template
on meeting certain criteria.
Ans: Yes we can get current user ID without apex by simply importing
import Id from ‘@salesforce/user/Id’
Ans: Both are used to compare two variables but == doesnt take data type into
consideration and does type coercion ie interpreter tries to automatically convert data
types to match the values while in === case if data type is not same it will always return
false.
For example :
2==”2” will return True (Type coercion happens)
2===”2” will return False ( No Type coercion)
Ans: It means simply when string literal is evaluated all the placeholders added into it are
calculated at run time and replaced in string with values. Place holder can be anything a
variable , expression even a function call. In javascript its performed using (backtick).
For example:
Whatsapp Chat
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 13/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
For example:
Ans: Yes
Ans: No we cant do DML inside any method annotated with cacheable = true , you will
receive an error as DMLLimit Exception.
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 14/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Ans: This error usually occurs when you are trying to make changes to a property which is
marked as @api , ideally you should clone the value then make the changes to it.
Ans: We cant add expressions directly in html file rather what we can do is create getter
which will evaluate value for us which we can use in html like below :
Ans: We cant put constants inside a class or function in javascript its illegal for example
below piece of code will throw you an error.
Ans: We can query all of them using one query selector only no need to write multiple for
each tag. We can pass all tags (,) separated to query all.
If you are wondering why are we using (…) spread operator before querySelectorAll its
because querySelector returns you the nodelist and using spread operator we convert it to
array of items otherwise we wouldnt be able to use array functions like map or reduce.
Whatsapp Chat
55. What is reduce method in JS?
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 15/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Reduce method calls the reducer function (callback function) for each item of the array to
reduce the whole array into single value.
Remember : If default value is passed accumulator is set to default value and index starts
with 0 , if no default value is passes accumulator is assigned 0th index value and
currentIndex starts from 1.
renderedCallback(){
this.statusVal = ‘ABC’
}
Ans: Yes you will receive an error “Maximum depth exceeded” as in set function you are
again setting statusVal itself which means it will keep on setting itself (infinitely).
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 16/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Answer: In Lightning Web Components (LWC), lifecycle hooks are special methods that
allow developers to execute code at specific points during a component’s lifecycle. Two of
the most commonly used lifecycle hooks are connectedCallback and
renderedCallback . These hooks provide opportunities to run code when a component is
inserted into the DOM and after it has been rendered, respectively. Understanding the
differences between these hooks is crucial for effectively managing component
initialization and updates.
Code Example:
renderedCallback() {
console.log('Component rendered');
}
}
Answer:
Code Explanation
1. Using Public Properties Public properties are used to pass data from Whatsapp
a parent Chat
component to a child component. The child component exposes a public property using
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 17/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
the @api decorator, and the parent component binds data to this property.
Code Example:
2. Using Custom Events Custom events are used to pass data from a child component
to a parent component. The child component dispatches an event with the data, and the
parent component listens for this event and handles the data.
Code Example:
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 18/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
handleClick() {
const event = new CustomEvent('myevent', { detail: 'Hello from Child' });
this.dispatchEvent(event);
}
}
3. Using Lightning Message Service (LMS) The Lightning Message Service (LMS) is
used to communicate between components that do not have a direct parent-child
relationship, even if they are in different parts of the application.
Code Example:
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 19/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
handleClick() {
const message = { message: 'Hello from Publisher' };
publish(this.context, SAMPLEMC, message);
}
disconnectedCallback() {
releaseMessageContext(this.context);
}
}
context = createMessageContext();
@wire(MessageContext)
messageContext;
connectedCallback() {
this.subscription = subscribe(
this.messageContext,
SAMPLEMC,
(message) => this.handleMessage(message)
);
}
disconnectedCallback() {
unsubscribe(this.subscription);
releaseMessageContext(this.context);
}
handleMessage(message) {
console.log('Received message:', message);
}
} Whatsapp Chat
// us
Call Subscriber Component
on: IN +91 93478 Template
54179 , 93467 (subscriberComponent.html)
64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 20/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
<template>
<p>Check console for received messages</p>
</template>
Answer:
Code Explanation
Code Example:
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 21/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Explanation:
1. Child Component:
Template: The child component’s template includes a button that triggers the
handleClick method when clicked.
2. Parent Component:
Event Listener: The parent component template listens for the myevent event
using the onmyevent attribute and maps it to the handleEvent method.
handleEvent Method: This method logs the received event detail to the console,
demonstrating how the parent component can react to events dispatched by the
child component.
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 22/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
Code Explanation
Using Promises and async / await JavaScript Promises represent the eventual
completion (or failure) of an asynchronous operation and its resulting value. The async /
await syntax is a modern way to work with Promises, allowing developers to write
asynchronous code that appears synchronous, making it easier to read and maintain.
Key Points:
Code Snippet:
async connectedCallback() {
this.data = await fetchData(); Whatsapp Chat
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 23/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
}
}
Explanation:
3. await Keyword: Pauses the execution of the connectedCallback function until the
fetchData Promise resolves. This ensures that this.data is assigned the fetched
data before the method completes.
By using async / await , the code becomes more readable and easier to maintain
compared to traditional Promise chaining with .then() and .catch() . This approach
ensures that asynchronous operations are handled efficiently, keeping the user interface
responsive and enhancing the overall user experience.
Answer:
Code Explanation
Using Template Directives if:true and if:false The if:true and if:false
Whatsapp Chat
directives in LWC are used to conditionally render parts of the template based on the
Call us on: IN +91 93478 54179 , 93467 64857 , USA +1 (415) 529-7271 || Request Call Back || Please call to USA number during USA day time.
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 24/27
05/06/2024, 16:32 61 LWC Lightning Web Component Interview Questions and Answers
value of a boolean property in the component’s JavaScript class. When the condition
evaluates to true, the element is rendered; otherwise, it is not included in the DOM.
Key Points:
Example Use Case: In the provided code example, the component uses the if:true
directive to render an element only when a boolean property ( isVisible ) is true. This
allows the developer to control the visibility of elements based on the component’s state.
<template if:true={isVisible}>
<p>This is visible</p>
</template>
<template if:false={isVisible}>
<p>This is hidden</p>
</template>
// JS
import { LightningElement, track } from 'lwc';
Explanation:
https://www.crsinfosolutions.com/61-lwc-lightning-web-component-interview-questions/ 25/27