Hello friends, In this post you will see a code example that helps you to get current user id in lightning web component(LWC), so let's get started,

Get current user id in lightning web component

To achieve this requirement, you don't need any apex code. We just need to import @salesforce/user/Id scoped module to get the current user id in LWC.

CurrentUserInfoLWC.html

 <template>
<lightning-card title="Current User Info" icon-name="standard:user">
<p>&nbsp;<b>Current User Id :</b> {currentuserId}</p>
</lightning-card>
</template>
CurrentUserInfoLWC.js
 import { LightningElement } from 'lwc';
import userId from '@salesforce/user/Id';
export default class CurrentUserInfoLWC extends LightningElement {
currentuserId = userId;
}
CurrentUserInfoLWC.js-meta.xml
 <?xml version="1.0"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>CurrentUserInfo</masterLabel>
<targets>
<target>lightning__RecordAction</target>
</targets>
</LightningComponentBundle>

Output:

 

Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.