Writing JavaScript Code, Used <script> Tag
The <script> tag is used to include JavaScript code within an HTML document that allows to define client-side scripts that can be executed by the web browser when the page is loaded or in response to specific events.
Syntax Example of the <script> Tag
<script>
//Write JavaScript code here
</script>
Type to Include JavaScript Code By Using <script> Tag
1. Inline JavaScript
You can directly write the JavaScript code within the <script> tags that approach is suitable for short code snippets.
Code Looklike
<script>
alert("Hello, world!");
</script>
2. External JavaScript
Instead of writing JavaScript directly in the HTML document that can also reference an external JavaScript file using the src attribute of the <script> tag.
Code Looklike
<script src="path/to/your-script.js"></script>
3. Inline Event Handlers
It was common to use inline event handlers directly within HTML attributes, like OnClick, OnMouseOver, etc. However, this practice is generally discouraged due to concerns over separation of concerns and maintainability.
Code Looklike
<button onclick=" alert('Button clicked!')">Click Here</button>