getting the size of img using javascript without the table part [closed]

hi so I’ve been working on some code.

<!DOCTYPE html>
<html>
    <body>
<table>
    <tr>
        <th id="th">
        </th>
    </tr>
</table>
            <img
            id="photo"
            src="../img/2.jpeg"
             width="30%" height="30%" 
            />
<script>
    var th = document.getElementById('th');
    alert(th.clientHeight);
    </script>
    <script>
    var img = document.getElementById('photo'); 
    var width=img.clientHeight;
    var height=img.clientHeight;
    alert(width+"--"+height);
        </script>
    </body>
</html>

the strange thing is, it works unless I remove the table part. Even the alert is enough.
does anybody have an idea what causes this?
and how to solve it?