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

Recently I needed to pass the current taxonomy ID for a category and sub category in WordPress. I came up with a variety of ways it worked and did not work. 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.