Summary
To create an inline script, place your JavaScript code directly inside a script element:
<script>
console.log("Test message");
</script>
Details
When writing JavaScript, you have the option of placing JavaScript code directly inside a script element in an HTML file. Please note, however, that this is generally not recommended. Inline scripts are acceptable for quick testing of JavaScript code, but the best practice is to store your JavaScript code in a separate JavaScript file.
Note that you do not need the src
attribute when creating an inline script.
Demo
Exercises
Try recreating the code snippet from the lesson in the index.html
file.
References
The Script element on MDN