Implement javascript inside body, error: jQuery is not defined
In Drupal 8 it is not a good idea at all. You must define a library and put your js in a seperate file.

Here is a link to how to do it

Libraries in Drupal 8

Then if you want to write jquery you just need to add a dependency on core/jquery, also explained on the link.

If you want to attach a library to a twig function, you can do like this:

{# In a Twig template we attach the library. #}
{{ attach_library('modulename/libraryname') }}