Uncaught SyntaxError: Unexpected token = in Google Chrome
12,140
Solution 1
You are using a default parameter feature which is supported only by Firefox now.
function errorNotification(text) {
text = text || "Something went wrong!";
$.pnotify({
title: 'Error',
text: text,
type: 'error'
});
}
Solution 2
Javascript does not allow you to pass default arguments like that. You need to assign the default internally to the function.
function errorNotification(text) {
text || (text = "Something went wrong!");
$.pnotify({
title: 'Error',
text: text,
type: 'error'
});
}
Solution 3
Note: The other answers won't work if you have a boolean value that you want to default to true
. That's because anything||true
will always return true
. Instead you should do something like:
function foo(bar){
if(bar === undefined)
bar = true;
//rest of your function
}
Comments
-
Anshad Vattapoyil almost 2 years
I have a javascript function which accept an optional parameter. This works fine in
Firefox
, but inGoogle Chrome
it shows:-Uncaught SyntaxError: Unexpected token =
My Code,
function errorNotification(text = "Something went wrong!") { $.pnotify({ title: 'Error', text: text, type: 'error' }); }
I have seen lot of similar questions but I can't realize my problem.