You wouldn't get that error in an HTML document.
Presumably, therefore, you are writing XHTML.
My first piece of advice is: Don't. XHTML is usually more trouble than it is worth. Use HTML 5 instead and that won't be invalid.
If you need to use XHTML then see the specification:
Differences from HTML: Script and Style elements
In XHTML, the script and style elements are declared as having #PCDATA
content. As a result, < and & will be treated as the start of markup,
and entities such as < and & will be recognized as entity
references by the XML processor to < and & respectively. Wrapping the
content of the script or style element within a CDATA marked section
avoids the expansion of these entities.
<script type="text/javascript">
<![CDATA[
... unescaped script content ...
]]>
</script>
HTML compatibility guidelines: Embedded Style Sheets and Scripts:
Use external style sheets if your style sheet uses < or & or ]]> or
> --. Use external scripts if your script uses < or & or ]]> or --. Note that XML parsers are permitted to silently remove the contents of
comments. Therefore, the historical practice of "hiding" scripts and
style sheets within "comments" to make the documents backward
compatible is likely to not work as expected in XML-based user agents.