I have the following code, and eslint keeps showing no-sequences
warning.
const full = {};
["firstname", "lastname", "spouse"].forEach(key => {
})
["cellphone", "phone"].forEach(key => {
})
Though the error is on the second forEach
block, the warning only appears when I put the first forEach
block. is this an eslint bug?
here is the link on eslint demo editor
Since your code has no semicolon, you're basically trying to access an array in the first forEach
:
["firstname", "lastname", "spouse"].forEach(key => {
})["cellphone", "phone"].forEach(key => {
})
Of course this is not correct. To fix this problem, just add a semicolon:
["firstname", "lastname", "spouse"].forEach(key => {
}); // Add a semicolon here
["cellphone", "phone"].forEach(key => {
}); // Here it is not necessary, but it is a good practice to avoid that kind of error
This will fix the error because semicolons point out the end of a statement, so ESLint will be able to understand that there are two different statements.
You are missing a semi-colon at the end of the first forEach
call, so your code is equivalent to:
["firstname", "lastname", "spouse"].forEach(key => {
})["cellphone", "phone"].forEach(key => {})
That is attempting to get the value of the "phone"
property of the return value of forEach
(which is undefined
). []
is interpreted to be bracket notation instead of an array literal due to automatic semicolon insertion. ESLint is pointing out the use of the comma operator in "cellphone", "phone"
, which results in attempting to read the "phone"
property.
I didn't know object value access is actually multiline.
const data = {"key":123}
console.log(
data
["key"]
)
Though on newline, Would actually work and print access data["key"]
to print 123
.