How to load a Taxonomy Term in Drupal 9

Two ways to load the term,
By using term id.
By using term name.

call the taxonomy class.

use Drupal\taxonomy\Entity\Term;

To load the term using term id,

$tid = 'Term Id';

$term = Term::load($tid);

To get the term name from the loaded term object,

$term->getName();

To load the term using name,

$name = 'Term Name';

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $name]);

Note: If two different vocabularies have terms with same name, then specify the vocabulary name in addition to load the desired term,

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $name, 'vid' => 'vocabulary_name']);

To get the tid from the loaded term object,

$term = reset($term);

$term_id = $term->id();

Category