Skip to content
NewRedesigned blogFeedback
Jigar Karangiya

Magento 2 - How to get customer data in custom js?

Hello Devs, In this tutorial we are going to see how we can get customer data in a custom js component.

Magento 2 - How to get customer data in custom js?
Written by
JKJigar Karangiya
Published
Reading time
1 min read

Jigar Karangiya · · 1 min read

Hello Devs, In this tutorial we are going to see how we can get customer data in a custom js component.

We can get customer data by injecting “Magento_Customer/js/customer-data ” using require function in our custom js/javascript component in Magento 2.

After injecting the object, we can use customerData.get(‘customer’) to get customer information if the customer is logged in.

Let’s see and understand it using the below example.

javascript
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
require(['Magento_Customer/js/customer-data'], function(customerData){
    var customer = customerData.get('customer');
    //console.log('customer);
    
    //your custom code here
    if(customer.firstname) {
        alert('Hello '+customer.firstname);
    }
});

You may also like:

How to use jQuery in Magento 2?

That’s it for this tutorial, I will see you in the next tutorial. Happy Coding 🙂

Keep reading
All posts

01 · 10 min · Shares Adobe Commerce Core

Adobe Commerce Agentic Capabilities, Q4 2026

Five agentic releases for Adobe Commerce: LLM product discovery, Brand Visibility, the Shopper MCP server, Brand Concierge, and Experience Workspace. What each one does, which editions get it, and when.

10 min

03 · 3 min · Shares Adobe Commerce Core

Conditions for Collections in Magento 2

Collections in Magento 2 (Now Adobe Commerce), are among the most powerful tools for retrieving and manipulating data directly from the database.

3 min

Newsletter

New Magento tutorials, straight to your inbox

Code examples, performance tips, and fixes for common Magento issues. No spam.

Subscribe