How To Get WordPress Current Category ID and even sub category ID

I was working on a project where I had to get the current WordPress category ID. This meant capturing the WordPress category ID if they were on a parent or child category.

I came up with a variety of ways where it would work in some instances, but not in all scenarios. Here are some details that should be helpful when you want to know the ID of the category the user is currently on.

How to Get WordPress Current Category ID and even sub category ID

Option 1: Get Category ID (Does Not Work with Subcategory)

<?php
if (is_category()){
#$categories = get_the_category();
#$category_id = $categories[0]->cat_ID;
echo $category_id;
}
?>

This will return the Category ID but it did not display the current ID if I was on a subcategory.

Option 2: Get Category ID & Subcategory ID

<?php
if (is_category()){
$category = get_queried_object();
$categorynew = $category->term_id;
echo $categorynew;
}
?>

Thanks to pagecrafter.com I used this because it allowed me to retrieve the category ID for categories and subcategories.