Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

SyntaxError: Reserved Word

DZone 's Guide to

SyntaxError: Reserved Word

If you accidentally use reserved words when coding in JavaScript, your code will throw an error. In this post, we look at how to fix this issue.

· Web Dev Zone ·
Free Resource

Remember when you were a mere beginner in JavaScript? While learning you must have come across the term, ‘reserved words.’ These are the words that you can’t use as names for variables. Apart from mainstream keywords like break, var, if…else, etc., there are many more reserved keywords.

SyntaxError: "x" is a reserved identifier (Firefox) 
SyntaxError: Unexpected reserved word (Chrome)

Above is the error messages you will receive if you use such words by accident. Seasoned JS developers, too, commit such blunders while coding.

Strict Mode

Strict mode has become a necessity because of its many benefits. It streamlines the code flow and allows for easy error debugging. I wouldn’t say that it has shortcomings, but you need to be extra careful while implementing this.

In addition to the already existing reserved keywords, strict mode reserves a few more keywords like implements, interface, let, package, private, protected, public, as, yield, and static.

<!DOCTYPE html> 
<html> 
	<body>   
		<h2>With "use strict":</h2> 
		<h3>Using a reserved word as variable name, is not allowed.</h3>   
		<p>Activate debugging in your browser (F12) to see the error report.</p>   
		<script> 
          "use strict"; var public = 'Thanos';  
          // This will cause an error (public is reserved in strict mode). 
        </script>   
	</body> 
</html>


Unexpected syntax error

If we change the variable name to avengerEnemy, it will work.

<!DOCTYPE html> 
<html> 
	<body>   
		<h2>With "use strict":</h2> 
		<h3>No reserved word used</h3>   
		<p>Activate debugging in your browser (F12) to see the avengerEnemy.</p>   
		<script> 
          "use strict"; var avengerEnemy = 'Thanos';  
          //  avengerEnemy is not a reserved word in strict mode. console.log(avengerEnemy); 
      </script>   
	</body> 
</html>


error debugging, reserved keywords

If you still get the same error, it may be due you be using an outdated version of your browser. Try updating the browser, as the old versions have old reserved words that need to be revised.

Happy coding!

Topics:
syntax errors ,javascript tutorial ,web dev ,javascript tutorial for beginners

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}