Is there an option to and/or how do I suppress errors like the following?
175,14:['tracker'] is better written in dot notation.
If it's a feature and not a bug, place this at the top of your file.
/*jshint sub:true*/
If it's a bug, you should refactor your code
foo['tracker'] = bar // from this...
foo.tracker = bar; // to this!
Good post on the reasons here: https://stackoverflow.com/a/2001410/94668
As suggested by: #ThorSummoner you can use below in your .jshintrc file
"sub": true
In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W069.
This means you can tell JSHint to not issue this warning with the /*jshint -W069 */ directive.
You can even wrap several lines of code and then reenable the warning as the example below (with a note to future you why it was a good idea):
/*jshint -W069 */
/*Disable Warning Justification:
Using bracket notation so Google Closure Compiler
ADVANCED_OPTIMIZATIONS will keep the original property names. */
obj['prop1'] ='foo';
obj['prop2'] ='bar';
/*jshint +W069 */
I assume you are asking about Dreamweaver or another editor.
Dreamweaver
You may go to Edit -> Preferences -> Linting
Select JS in the dropdown and hit Edit & Apply changes
Find
"sub": false,
and change that to true. Save the file and that notice will disappear.
If you have OTHER Linting things you wish to edit, you can find a helpful list of them all at https://jshint.com/docs/options/
What's the correct way to prevent warnings on IntelliJ, for example this code:
the .children_str gives warning Unresolved variable children_str. The children_str property is a string, and row is an Object.
Normally I would add a namespace (I don't know what is it, but it removes those warnings):
But then another warning appeared Unresolved function or method split()
What's the correct way to tell IntelliJ that children_str is a string to remove those warning message?
You can hit Alt+Enter to see Idea's way to resolve this warning. If there are none, you can always disable JavaScript Inspections. There are two ways:
Alt+Enter then click right arrow and select Disable Inspection
Go to File -> Settings -> Inspections and find JavaScript inspections options. There you can disable any inspections that annoy you, like Unresolved JavaScript function or Unresolved JavaScript variable
The warning can be removed by assigning and adding || operator, for example:
row.children_str = row.children_str || '';
all
when i config my Webstorm9 settings like this ,it turns out that the sample code in the right aligns the equals sign properly
but when i formatting my javascript code in the editor it doesn't work,it still shows
and what i want is align the equals sign
anyone who can take a look?
-------EDIT------
maybe i misunderstand the checked option,but i copy the sample code to my editor then reformat it, oh,bad result..
In Webstorm 10,
Go to Settings> Editor> Code Style > JavaScript
Go to the "Other" tab and check "Align multiple 'var' statements and assignment" and click Ok.
Go to Code > Reformat code (Ctrl + Alt + L)
Voila!
In PHPStorm 2017 (and, I assume, by extension WebStorm) that behavior can be achieved by:
going to Settings > Editor > Code Style > JavaScript
Wrapping and Braces tab
scroll down to Variable declarations and set Align to When Grouped
Additionally, setting Variable declarations to Wrap always will result in this when reformatting:
Update: unchanged in version 2018 and in WebStorm/PHPStorm version 2019, the steps I described above still work the same.
Update for WebStorm 2017.3
Apparently, since version 2017.3, the Other tab has been removed from Code Style > JavaScript.
Now you have a couple of alignment options under the Wrapping and Braces tab (scroll down to the bottom):
It took me a while to figure this out, hope this helps
Update for Webstorm 2016.1
#Hudvoy still has the right answer for this. I've updated with a visual of where you need to look in Webstorm 2016.1
Take note that the exact phrasing of the option is
Align multiline variable declaration
(emphasis mine) and that the example code shows:
var myLink = {
....
},
local = true,
initial = -1;
This option only aligns the equal signs of multiline variable declarations, not a bunch of single line variable declarations in a row like in your code.
To have it automatically align them you would need to change your code to
a = 1,
bbbbb = 2,
thisisc = 3;
Note that you also have to add a var before it all, as WebStorm will assume a=1 to be a reference to a global variable, not a variable definition.
Also note that code formatting is not automatic. You have to select Code->Reformat Code... from the menu, or hit Ctrl + Alt + L to reformat your code.
I also found that Webstorm could perform code formatting on the Javascript within either a .html or .js file, but would not work within a .php file - in PHP files it only performs syntax highlighting.
Is there an option to and/or how do I suppress errors like the following?
175,14:['tracker'] is better written in dot notation.
If it's a feature and not a bug, place this at the top of your file.
/*jshint sub:true*/
If it's a bug, you should refactor your code
foo['tracker'] = bar // from this...
foo.tracker = bar; // to this!
Good post on the reasons here: https://stackoverflow.com/a/2001410/94668
As suggested by: #ThorSummoner you can use below in your .jshintrc file
"sub": true
In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W069.
This means you can tell JSHint to not issue this warning with the /*jshint -W069 */ directive.
You can even wrap several lines of code and then reenable the warning as the example below (with a note to future you why it was a good idea):
/*jshint -W069 */
/*Disable Warning Justification:
Using bracket notation so Google Closure Compiler
ADVANCED_OPTIMIZATIONS will keep the original property names. */
obj['prop1'] ='foo';
obj['prop2'] ='bar';
/*jshint +W069 */
I assume you are asking about Dreamweaver or another editor.
Dreamweaver
You may go to Edit -> Preferences -> Linting
Select JS in the dropdown and hit Edit & Apply changes
Find
"sub": false,
and change that to true. Save the file and that notice will disappear.
If you have OTHER Linting things you wish to edit, you can find a helpful list of them all at https://jshint.com/docs/options/
Some users are reporting occasional JS errors on my site. The error message says "Expected identifier, string or number" and the line number is 423725915, which is just an arbitrary number and changes for each report when this occurs.
This mostly happens with IE7/ Mozilla 4.0 browsers.
I scanned my code a bunch of times and ran jslint but it didn't pick anything up - anyone know of the general type of JS problems that lead to this error message?
The cause of this type of error can often be a misplaced comma in an object or array definition:
var obj = {
id: 23,
name: "test", <--
}
If it appears at a random line, maybe it's part of an object defintion you are creating dynamically.
Using the word class as a key in a Javascript dictionary can also trigger the dreaded "Expected identifier, string or number" error because class is a reserved keyword in Internet Explorer.
BAD
{ class : 'overlay'} // ERROR: Expected identifier, string or number
GOOD
{'class': 'overlay'}
When using a reserved keyword as a key in a Javascript dictionary, enclose the key in quotes.
Hope this hint saves you a day of debugging hell.
Actually I got something like that on IE recently and it was related to JavaScript syntax "errors". I say error in quotes because it was fine everywhere but on IE. This was under IE6. The problem was related to JSON object creation and an extra comma, such as
{ one:1, two:2, three:3, }
IE6 really doesn't like that comma after 3. You might look for something like that, touchy little syntax formality issues.
Yeah, I thought the multi-million line number in my 25 line JavaScript was interesting too.
Good luck.
This is a definitive un-answer: eliminating a tempting-but-wrong answer to help others navigate toward correct answers.
It might seem like debugging would highlight the problem. However, the only browser the problem occurs in is IE, and in IE you can only debug code that was part of the original document. For dynamically added code, the debugger just shows the body element as the current instruction, and IE claims the error happened on a huge line number.
Here's a sample web page that will demonstrate this problem in IE:
<html>
<head>
<title>javascript debug test</title>
</head>
<body onload="attachScript();">
<script type="text/javascript">
function attachScript() {
var s = document.createElement("script");
s.setAttribute("type", "text/javascript");
document.body.appendChild(s);
s.text = "var a = document.getElementById('nonexistent'); alert(a.tagName);"
}
</script>
</body>
This yielded for me the following error:
Line: 54654408
Error: Object required
Just saw the bug in one of my applications, as a catch-all, remember to enclose the name of all javascript properties that are the same as keyword.
Found this bug after attending to a bug where an object such as:
var x = { class: 'myClass', function: 'myFunction'};
generated the error (class and function are keywords)
this was fixed by adding quotes
var x = { 'class': 'myClass', 'function': 'myFunction'};
I hope to save you some time
http://closure-compiler.appspot.com/home will pick this error up with an accurate reference to the actual line number in the offending script.
As noted previously, having an extra comma threw an error.
Also in IE 7.0, not having a semicolon at the end of a line caused an error. It works fine in Safari and Chrome (with no errors in console).
IE7 is much less forgiving than newer browsers, especially Chrome. I like to use JSLint to find these bugs. It will find these improperly placed commas, among other things. You will probably want to activate the option to ignore improper whitespace.
In addition to improperly placed commas, at this blog in the comments someone reported:
I've been hunting down an error that only said "Expected identifier"
only in IE (7). My research led me to this page. After some
frustration, it turned out that the problem that I used a reserved
word as a function name ("switch"). THe error wasn't clear and it
pointed to the wrong line number.
Remove the unwanted , sign in the function. you will get the solution.
Refer this
http://blog.favrik.com/2007/11/29/ie7-error-expected-identifier-string-or-number/
This error occurs when we add or missed to remove a comma at the end of array or in function code. It is necessary to observe the entire code of a web page for such error.
I got it in a Facebook app code while I was coding for a Facebook API.
<div id='fb-root'>
<script type='text/javascript' src='http://connect.facebook.net/en_US/all.js'</script>
<script type='text/javascript'>
window.fbAsyncInit = function() {
FB.init({appId:'".$appid."', status: true, cookie: true, xfbml: true});
FB.Canvas.setSize({ width: 800 , height: 860 , });
// ^ extra comma here
};
</script>
This sounds to me like a script that was pulled in with src, and loaded just halfway, causing a syntax error sine the remainder is not loaded.
IE7 has problems with arrays of objects
columns: [
{
field: "id",
header: "ID"
},
{
field: "name",
header: "Name" , /* this comma was the problem*/
},
...
Another variation of this bug: I had a function named 'continue' and since it's a reserved word it threw this error. I had to rename my function 'continueClick'
Maybe you've got an object having a method 'constructor' and try to invoke that one.
You may hit this problem while using Knockout JS. If you try setting class attribute like the example below it will fail:
<span data-bind="attr: { class: something() }"></span>
Escape the class string like this:
<span data-bind="attr: { 'class': something() }"></span>
My 2 cents.
I too had come across this issue. I found below two solutions.
1). Same as mentioned by others above, remove extra comma from JSON object.
2). Also, My JSP/HTML was having . Because of this it was triggering browser's old mode which was giving JS error for extra comma. When used it triggers browser's HTML5 mode(If supported) and it works fine even with Extra Comma just like any other browsers FF, Chrome etc.
Here is a easy technique to debug the problem:
echo out the script/code to the console.
Copy the code from the console into your IDE.
Most IDE's perform error checking on the code and highlight errors.
You should be able to see the error almost immediately in your JavaScript/HTML editor.
Had the same issue with a different configuration. This was in an angular factory definition, but I assume it could happen elsewhere as well:
angular.module("myModule").factory("myFactory", function(){
return
{
myMethod : function() // <--- error showing up here
{
// method definition
}
}
});
Fix is very exotic:
angular.module("myModule").factory("myFactory", function(){
return { // <--- notice the absence of the return line
myMethod : function()
{
// method definition
}
}
});
This can also happen in Typescript if you call a function in middle of nowhere inside a class. For example
class Dojo implements Sensei {
console.log('Hi'); // ERROR Identifier expected.
constructor(){}
}
Function calls, like console.log() must be inside functions. Not in the area where you should be declaring class fields.
Typescript for Windows issue
This works in IE, chrome, FF
export const OTP_CLOSE = { 'outcomeCode': 'OTP_CLOSE' };
This works in chrome, FF, Does not work in IE 11
export const OTP_CLOSE = { outcomeCode: 'OTP_CLOSE' };
I guess it somehow related to Windows reserved words