drupal get user id

Drupal 8 Get Current User ID

The following command must be used to get the current user id for drupal 8 and drupal 9.

\Drupal::currentUser()->id();