JavaScript unexpected token syntax error [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
What might be causing this error?
My code:
var a = ()
var s = 0
if (a == s) {
console.log('in')
}
The error:
SyntaxError { "Unexpected token" (1:23) }

var a = [];
var s = 0;
if (a == s) {
console.log('in');
}
Why is this being caused? - The characters () right now don't match the syntax of any normal use case of parenthesis. The ways to use parenthesis are here:
define a function: var a = function() { console.log("foo")}
call a function: var a = Math.random(1)
define an arrow function: var a = () => console.log("foo")
wrap any kind of expression: var a = (true && false)
Since in your code's syntax doesn't fit any of those it is giving a Syntax error.
The code above makes the code work using []. [] creates an empty

Related

How to avoid 'Uncaught ReferenceError: ? is undefined'? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a script (config.js) at the base of every page. But, that script contains code that isn't used on every page. The console returns unused or undeclared variables as undefined.
let currentURL = document.location.href;
function redirectURL() {
if (currentURL.indexOf('dogs.html') > -1) {
redirect.innerHTML = `Cats`;
} else {
redirect.innerHTML = `Dogs`;
}
}
redirectURL();
This is what's returned.
How do I fix this?
You need to check whether the redirect element exists before trying to use it.
function redirectURL() {
if (typeof redirect == 'undefined') {
return;
}
if (currentURL.indexOf('dogs.html') > -1) {
redirect.innerHTML = `Cats`;
} else {
redirect.innerHTML = `Dogs`;
}
}
This will allow the function to work without error on pages that don't define redirect.

How to Change or replace a part of URL string in javascript? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have one url which contains special character like
#1# or #2#
http://10.10.10.10:8000/admin/taskinstance/?flt1_dag_id_equals=#1#&flt2_state_equals=#2#
I want to change with some other value like in place of #1# I need "hello" and In place of #2# I need "there", I tried with some code, but it's not working
var str = response.data;
var mapObj = {
#1#:dagId,
#2#:state
};
str = str.replace(/#1#|#2#/gi, function(matched){
return mapObj[matched];
});
In response.data I am getting same URL which I have mention above,
I am getting:
SyntaxError: Invalid or unexpected token for line no 3.
Is something is wrong in syntax?
I am fine with any syntax angularjs or javascript.
You have to change the regex a bit by wrapping strings with parenthesis. Your object is not valid as well:
var str = 'http://10.10.10.10:8000/admin/taskinstance/?flt1_dag_id_equals=#1#&flt2_state_equals=#2#';
var mapObj = {
"#1#":"dagId",
"#2#":"state"
};
str = str.replace(/(#1#)|(#2#)/gi, function(matched){
return mapObj[matched];
});
console.log(str)

Map each value of an array to a given function to return a new array in JavaScript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
The code below throws the following exeption:
Uncaught TypeError: someFunction(...) is not a function
What is causing this exception to be thrown?
map = function (someList, someFunction){
var result =[];
for (x = 0; x < someList.length; x++ ){
result.push(someFunction(someList[x])());
}
return result;
};
map([1,2,3,4], function(num){
return num * 10;
});
The problem is in this line
result.push(someFunction(someList[x])());
^^
This extra parentheses are redundant. You are already calling the function someFunction by saying someFunction(someList[x]). By adding these extra parentheses, you are basically trying to call the return value of someFunction(someList[x]), which is a number here, not a function.

If/Else only works for first if [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working with this, but the issue is that only the first if ever functions correctly. If I change the order, each if statement works on its own, but the logic where if the first is false, then check the second, and so on isn't working. What am I missing here?
$("#search_button").click(function(){
var table = $('#main_index').DataTable();
var search_term = $("#second_select2 option:selected").text();
var first_s = $("#first_select2 option:selected").text();
if (first_s = 'District'){
table.columns(1).search(search_term).draw();
}
else if (first_s = 'Territory'){
table.columns(2).search(search_term).draw();
}
else if (first_s = 'Region'){
table.columns(0).search(search_term).draw();
}
else {
console.log('error');
}
});
If I console.log the search_term and first_s variables, I can see them changing and correctly working. And, as I said, without the if/else statements each of these works on its own.
In all your comparisons, you need to replace = by ===
You are using the assignment operator (=) in your comparisons. you should be using the identity operator: ===.
if (first_s = 'District'){
needs to be
if (first_s === 'District'){
as do the rest of the checks.

jQuery error = SyntaxError: missing : after property id [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
There seems to be a lot of these errors on SO but I can't find any to relate to.
The error is: SyntaxError: missing : after property id and it's complaining about this line: var msg = ""; (Line 3).
function checkSubmission()({
var msg = "";
$('#validation').html(msg);
if ($("#lms_name").val().length<=0){
msg+="LMS Name Required.<br/>";
}else{
var value = $('#lms_name').val();
var regex = new RegExp(/^[a-zA-Z0-9 ]{2,}$/);
if(value.match(regex)) {
} else {
msg+="<b>Error on LMS name : </b>Numbers and letters only. Minimum of two characters.<br/>";
}
}
//lots more code here...
Any ideas? FYI I'm a complete newbie (for now...).
Thanks in advance!
The code in your comment on #Shoaib Raza, prompted me to post this. (please accept his answer rather than mine, since his was first.)
Quote OP Comment:
FYI, I changed it to just function checkSubmission( var msg = ""; ... and the error still appeared.
And that's totally wrong too. checkSubmission( ... is not what his answer is showing, so the new error message is accurate: SyntaxError: missing ( before { -> function checkSubmission{
This line of your original code contains the original syntax error...
function checkSubmission()({
should be this...
function checkSubmission() {
Notice the difference? You simply need to remove the extra ( and nothing else.
Change your function to :
function checkSubmission()
{
var msg = "";
$('#validation').html(msg);
if ($("#lms_name").val().length<=0)
{
msg+="LMS Name Required.<br/>";
}
else
{
var value = $('#lms_name').val();
var regex = new RegExp(/^[a-zA-Z0-9 ]{2,}$/);
if(value.match(regex))
{
}
else
{
msg+="<b>Error on LMS name : </b>Numbers and letters only. Minimum of two characters.<br/>";
}
}
//lot more code in the function here.. if any
}
Hope this helps.

Categories

Resources