What is the difference between "lang" and "type" attributes in a script tag?

The lang and type attributes in script tags serve different purposes, though both relate to specifying the scripting language. Understanding their differences is important for modern web development.

The lang Attribute (Deprecated)

The language attribute was used in older HTML versions to specify the scripting language. It's now deprecated and should not be used in modern web development.

<script language="javascript">
    document.write("This is deprecated");
</script>

The type Attribute (Current Standard)

The type attribute is the modern, recommended way to specify the scripting language using MIME types. For JavaScript, use "text/javascript" or omit it entirely since JavaScript is the default.

<script type="text/javascript">
    console.log("Hello World!");
</script>

Modern Best Practice

In HTML5, the type attribute can be omitted entirely since JavaScript is the default scripting language:

<script>
    console.log("Modern approach - no type needed");
</script>

Complete Example

<!DOCTYPE html>
<html>
<head>
    <title>Script Attributes Example</title>
</head>
<body>
    <h1>JavaScript Examples</h1>
    
    <!-- Modern approach (recommended) -->
    <script>
        document.getElementById("output").innerHTML = "Hello from modern JavaScript!";
    </script>
    
    <!-- With type attribute (optional but valid) -->
    <script type="text/javascript">
        console.log("Hello from typed JavaScript!");
    </script>
    
    <div id="output"></div>
</body>
</html>

Comparison

Attribute Status Usage Recommendation
language Deprecated Old HTML versions Don't use
type Valid HTML4, XHTML, HTML5 Optional in HTML5
No attribute Valid HTML5 default Recommended

Key Points

  • The language attribute is deprecated and should not be used
  • The type attribute is valid but optional in HTML5
  • Modern browsers assume JavaScript by default when no type is specified
  • Use type="module" for ES6 modules

Conclusion

Use the type attribute only when necessary (like for modules). For regular JavaScript, omit both attributes as modern HTML5 defaults to JavaScript. Avoid the deprecated language attribute entirely.

Updated on: 2026-03-15T21:16:49+05:30

351 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements