In this post I am going to explain how to get customer data by customer id Magento 2.

Using below code snippet you can get customer data by passing customer id.

Add below code snippet in Block class.

protected $_customerRepository;
    
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
    array $data = []
) {
    $this->_customerRepository = $customerRepository;
    parent::__construct($context, $data);
}

// Get customer data
public function getCustomerData($customerId)
{   
    return $this->_customerRepository->getById($customerId);
}

Add below code snippet in template file.

// Customer ID
$customerId = 1;

// Call getCustomerData function
$customerData = $block->getCustomerData($customerId);

//customer first name
echo $customerData->getFirstname().'<br/>';

//customer last name
echo $customerData->getLastname().'<br/>';

//customer email
echo $customerData->getEmail();

// Customer All Data in Array

echo "<pre>";
print_r($customerData->__toArray());
echo "</pre>";