function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Inbox OutboxInbox Outbox 

I would like to know the difference (variable) between the below js files from three components in LWC


I have hightlighted the code with bold letters where I have doubt. 
My question is ,why is the variable defined sometimes above the class definition and sometimes within the class.
(the only difference I found is: variables are defined outside the close when it is standard fields)
Variables in question: objectApiName, fields and FIELDS.

My question is only about defining the variables. 
Excuse the way I put my question,  I did not know how to put it  into words. 
JS file 1: 
import { LightningElement, wire } from 'lwc';
import {getObjectInfo, getObjectInfos} from 'lightning/uiObjectInfoApi'
import ACCOUNT_OBJECT from '@salesforce/schema/Account'
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity'
export default class GetObjectInfoDemo extends LightningElement {
    @wire(getObjectInfo, {objectApiName:ACCOUNT_OBJECT})
    objectInfo
    objectApiNames = [ACCOUNT_OBJECT, OPPORTUNITY_OBJECT]
    objectInfos
    @wire(getObjectInfos, { objectApiNames: '$objectApiNames' })
    objectInfosHandler({data}){
        if(data){
            console.log(data)
            this.objectInfos = data
        }
    }
}

JS file 2: 
import { LightningElement, wire } from 'lwc';
import {getRecord} from 'lightning/uiRecordApi'
import Id from '@salesforce/user/Id'
import NAME_FIELD from '@salesforce/schema/User.Name'
import EMAIL_FIELD from '@salesforce/schema/User.Email'
const fields = [NAME_FIELD, EMAIL_FIELD]
export default class WireDemoUserDetail extends LightningElement {
    userId = Id
    userDetail
    @wire(getRecord, {recordId:'0050p000002MXU6AAO', fields})
    userDetailHandler({data, error}){
        if(data){
            this.userDetail = data.fields
        }
        if(error){
            console.error(error)
        }
    }
    @wire(getRecord, {recordId:'0050p000002MXU6AAO', fields})
    userDetailProperty
}

JS file 3: 

import { LightningElement, api, track, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecord } from 'lightning/uiRecordApi';
const FIELDS = ['Contact.Name', 'Contact.Phone'];
export default class LoadContact extends LightningElement {
    @api recordId;
    @track contact;
    @track name;
    @track phone;
    @wire(getRecord, { recordId: '$recordId', FIELDS })
    wiredRecord({ error, data }) {
        if (error) {
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error loading contact',
                    message: error.message,
                    variant: 'error',
                }),
            );
        } else if (data) {
            this.contact = data;
            this.name = this.contact.fields.Name.value;
            this.phone = this.contact.fields.Phone.value;
        }
    }
}
Best Answer chosen by Inbox Outbox
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please refer to the below link:- https://salesforce.stackexchange.com/questions/336935/why-declare-variables-outside-of-lwc-class

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi