\n\n\nLeaner syntax like this is totally compliant and recommended for coding in the modern Web world.","image":"https://explain.codes/media/static/images/eightify-logo.svg","author":{"@type":"Person","name":"Anton Shumikhin","url":"https://explain.codes//author/anton-shumikhin"},"publisher":{"@type":"Organization","name":"Rational Expressions, Inc","logo":{"@type":"ImageObject","url":"https://explain.codes/landing/images/[email protected]"}},"datePublished":"2025-01-16T20:00:02.171Z","dateModified":"2025-01-16T20:00:04.720Z"}
Explain Codes LogoExplain Codes Logo

Do HTML5 Script tags need type="javascript"?

html
best-practices
web-development
responsive-design
Anton ShumikhinbyAnton Shumikhin·Jan 16, 2025
⚡TLDR

In HTML5 there's no need for the type attribute for JavaScript in <script> tags, since it's the default. The new norm is to omit type, as shown below:

<script> // JavaScript code // (don't worry, it's not rocket science) </script>

Leaner syntax like this is totally compliant and recommended for coding in the modern Web world.

The evolution of web standards

We've reached a point where specifying "text/javascript" for the type attribute of <script> elements has been deemed as antiquated, largely due to the prevalence of JavaScript. This language has emerged as the dominant scripting language for the Web.

Browser behavior: Script execution protocol

Standard browsers work using a script execution protocol that assumes JavaScript as the default scripting language. Therefore, unless it's explicitly stated otherwise by a different MIME type, the inclusion of "text/javascript" is redundant.

Charset encoding: Are they necessary?

The need for charset declaration (<script charset="...">) within script tags has also become obsolete as the scripts are universally expected to be delivered in UTF-8 encoding. This is in alignment with the global transition towards UTF-8 as the primary character encoding for the Web.

When type can still hang around

While omission is the best practice, specifying type does no harm. There might be legacy systems or specific use cases that still require the attribute to function as expected. However, for most contemporary web development scenarios, it's safe to leave the type attribute out with confidence.

Best practices for script elements

Usage of external scripts

For external scripts, using the src attribute, culling the type="text/javascript" gives you:

<script src="path/to/your-favorite-script.js"></script>

This way, you avoid the equivalent of telling a pizza place you want a pizza.

Payload delivery: Inline scripts

Inline scripts also subscribe to this best practice. The simpler and uncluttered, the better:

<script> // Here be dragons (well, JavaScript really) </script>

Remember: a well-organized captain runs a tight ship.

Special script types for specific situations

There will be unique cases where non-standard script types like "text/babel" or "module" are mandatory. Such situations require explicit declaration of the type for ascertained functionality.