Get Website id from website code Magento 2.

Magento 2, You can retrieve Website id from the website code. Sometimes you need website id from the code, You have website code already exists but need website id, You can get it simply with Magento.

Use Interface:
Magento\Store\Api\WebsiteRepositoryInterface

Use method from the interface:
public function get($code);

<?php
namespace Jesadiya\WebsiteId\Model;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Store\Api\WebsiteRepositoryInterface;

class WebsiteId
{
    /**
     * @var WebsiteRepositoryInterface
     */
    public $websiteRepository;

    /**
     * @var LoggerInterface
     */
    public $logger;

    public function __construct(
        LoggerInterface $logger,
        WebsiteRepositoryInterface $websiteRepository
    ) {
        $this->logger = $logger;
        $this->websiteRepository = $websiteRepository;
    }

    /**
     * get website id
     *
     * @param string $code
     * @return int|null
     */
    public function getWebsiteId(string $code): ?int
    {
        $websiteId = null;
        try {
            $website = $this->websiteRepository->get($code);
            $websiteId = (int)$website->getId();
        } catch (Exception $exception) {
            $this->logger->error($exception->getMessage());
        }
    }
}

Call from the template or any PHP class,
$code = “base”;
echo $websiteId = $this->getWebsiteId($code);

Output:
(int)1
If Website not found return null.

Above is the example of Retrieve Website Id from the website code Magento 2 and  Get Website id from the base website code.