There are 2 common ways to trigger the SCRIPT1028: Expected identifier, string or number JavaScript error in earlier versions of Internet Explorer. The first way is to include a trailing comma after your last property in a JavaScript object. The other common trigger is using a JavaScript reserved word as a property name. I’ll show you examples of both, and how to work around some of these limitations.
Example #1: Trailing Comma
You’ll notice in this example, we leave a comma after the 'Login Successful' value. This will trigger the SCRIPT1028: Expected identifier, string or number error in Internet Explorer 7 and below.
var message = {
title: 'Login Unsuccessful',
};
Example #2: Using a JavaScript Reserved Word
In this example, we assign a property named class to our JavaScript object. Class is a reserved word, and cannot be used as an identifier.
var message = {
class: 'error'
};
The Solution
The solution is to pass the class property value as a string. You will need to use bracket notation, however, to call the property in your script.
var message = {
title: 'Login Unsuccessful',
text: 'Please check your username or password',
'class': 'error'
};
console.log(message.title);
console.log(message['class']);
Related Links
- MDN | Reserved Words – JavaScript
- https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Reserved_Words
