Uncaught SyntaxError: Unexpected token ) when using void() - javascript

I get this error and I've managed to narrow it down to:
aaa
That line of code is now the only thing in my source code and still I get the error in title. Any idea why so?
Even when surrounded with the appropriate HTML elements (html, head, body etc) I am still thrown the error. The error shows up in Chrome dev console and via alert if I include a
window.onerror
function in the head tag. It also occurs when the myFunction() method actually exists. As far as I can gather, there is absolutely nothing wrong with that above statement whatsoever.

Use
aaa
void expects a parameter.
There's an interesting discussion on using void(0) or other techniques here.

Is because void takes one argument.
You want:
aaa

void is an operator, not a function. It requires a single expression as its operand. () is not a valid expression. The correct syntax is:
aaa
You can put parentheses around 0, but they're not necessary, just as you don't need parentheses around 0 when writing 3 + 0.

Related

NodeJS thinks I am duplicating parameter names when I am not

Thanks for looking at my question.
In this javascript initialization code, on line 94, I am getting a syntax error: "Duplicate parameter name not allowed in this context". However, I am not duplicating any parameters. All of my functions' parameters names are unique within their scope.
The repository is at https://github.com/allenchan3/foodproject/blob/c3442a3b8542e1f9cbcc5f3f78175765a292dd9a, and the script in question is at https://github.com/allenchan3/foodproject/blob/c3442a3b8542e1f9cbcc5f3f78175765a292dd9a/server/config/initialize.js. The error is appearing on the function call to create_menu_items. I carefully inspected this file for duplicate parameter names but found none. I tried changing the names of each of the 3 declared variables in the main function, along with changing the names of the parameters. Nothing seems to get rid of the syntax error, which is preventing my function from executing.
async function create_menu_items(filenames, directory, cat_names_to_ids) {
/// stuff
}
async function main() {
await create_menu_items(menu_item_filenames, menu_item_dir, categories_name_to_id);
}
[skyler#laptop server]$ npm start
[.....snip.....]
(node:6571) UnhandledPromiseRejectionWarning: SyntaxError: Duplicate parameter name not allowed in this context
As mentioned, this error keeps appearing and the function create_menu_items doesn't run, even though I think it should, because none of the parameters appear to be in conflict with anything.
Thanks again for taking a look.
Here's your problem
objects.reduce((prev_items,curr_items_obj,_,_)=>{
^ ^
It seems that you wanted to omit optional parameters this way, but you should just skip them like this:
objects.reduce((prev_items,curr_items_obj)=>{
If you really care to use _ for omited parameters, name the other one as __ (double underscore) to avoid duplicate parameter error, as such:
objects.reduce((prev_items,curr_items_obj,_,__)=>{

Why does this code throw ReferenceError: test is not defined?

var te‌st = 1
console.log(test)
Try running this simple code. It gives error: ReferenceError: test is not defined, despite the fact that I defined that variable. Why is this happening?
In the variable declaration, the variable name contains a zero-width non-joiner (ZWNJ) character (between e and s), which is invisible, because its width is equal to zero. However, ECMAScript specification allows this character as a part of variable name.
However, in the console.log() call, there is just test, without any special characters. Therefore, it throws Reference Error, because the variable name is te<ZWNJ>st, not test.
Fortunately, there's an easy way to check if a variable name contains such characters. You can paste your code into JS Bin or JS Fiddle — they denote these characters with a white dot on a red background. That's how it looks like in JS Fiddle:
I think there are also similar features in some IDEs.
Side note: this is an interesting way to prevent people from copy pasting the code snippets you use in answers into their own code. Consider the following code snippet:
// Warning: non-copy-pastable, it won't work if you copy it into your code.
function a‌dd(a, b) {
return a + b
}
console.log(a‌dd(2, 3))
There's a ZWNJ character in the function name and the function call, so it works here. However, if someone copied the function into their code and then manually typed console.log(add(3, 4)), it would throw ReferenceError: add is not defined.
Please don't take the above seriously, it's rather a joke than a practical use.
Related
What characters are valid for JavaScript variable names?
No visible cause for “Unexpected token ILLEGAL”

Uncaught TypeError: object is not a function on "(function($)"

Why do I get the following error...
Uncaught TypeError: object is not a function
...at the following line, of a certain JS script?
(function($){
And why do I get that error only when JS are concatenated? (I'm using Gulp)
And why does it work if I add ; before that line, like that:
;(function($){
?
update
The preceding line - that is, the object which is not a function, according to the runtime error - on the concatened script was a }, as in:
storage = {
//...
}
I'm used to always put semicolon, but not after curly braces.
Turns out the curly braces could delimit the end of a statement, like in this case, and then it's recommended to use the semicolon to avoid this error. Here's a good explanation.
Javascript ignore missing semi-colon and try to interpret it. So if you don't input the semi-colon, it use the next line to see if it should end the line or chain it.
That allow you to use thing like this :
String
.split();
and it will be interpreted like that :
String.split();
But, this would also work :
String
.split
();
Now, If you have something like this :
var a = 'a';
var b = a
(function(){})
JavaScript has no way to know what you really want to do, so it will interpret it like that :
var a = 'a';
var b = a(function(){});
Giving you the error [place object type here] is not a function
Bottom line, always put your semi-colon.
Edit
After seeing your code, here how it is interpreted :
storage = {/**/}(function($){})(jQuery);
So Object ({} === Object) is not a function
When concatenated it believes you're trying to call whatever precedes the (function($) {...}.
If you put () after a reference it tries to call whatever the reference is. This is why you'll see a lot of JavaScript libraries precede their code with a lone ;

jshint complains about shorthand AND operator with boolean

I've got a javascript function that needs to be executed based on a boolean value. I really like to use the && operator for this (which only executes the second part if the first results in true).
someBoolean && executeFunction();
However, when JSHint checks my code, I get the following message:
Expected an assignment or function call and instead saw an expression.
I'm wondering why JSHint throws this message. I know I can easily avoid it by using a simple if statement, but I really like the simplicity of this one. Maybe I need to fiddle a bit with some JSHint configuration? Is there some hidden danger in this line of code?

Why is the syntax error in the eval() code ignored?

In function code, when I do this:
eval( 'var default = 100;' );
alert( default );
the browser's JS engines* will throw a Syntax Error on the second statement, as if the first statement executed successfully.
See here: http://jsfiddle.net/4FMdy/ (open the browser's console to view the error log)
However, when I remove the second statement from the code, so that I only have this:
eval( 'var default = 100;' );
the browser's JS engines will throw a Syntax Error on that statement.
See here: http://jsfiddle.net/4FMdy/1/
I don't understand this. If the first statement throws a syntax error (as it should), why does only the second statement throw such an error in my first example above. From what I understand, if a statement throws an error, that necessarily means that all previous statements (of the same call) executed successfully.
Btw, the syntax error is thrown because default is a reserved word in JavaScript, so it cannot be used as a variable name.
Also, no "eval is evil" comments please. I'm just trying to understand the behavior of the browser's JS engines.
* I tested in Firefox, and Chrome
Just an assumption:
The script-block first will be parsed, but not executed(the eval)
The browser detects only the syntax-error in line#2 and didn't execute complete script-block, so he will not determine the syntax-error in eval()
When you put the 2 lines in 2 different script-element you'll receive both errors:
http://jsfiddle.net/doktormolle/CfRmj/
Because "default" is Javascript reserved keyword :)
So alert(default) throws "syntax error" if code is checked if is correct, but then the function eval is runned, and process wount get here because of first error.

Categories

Resources