Chinmay Sarupria Chinmay Sarupria - 3 months ago 96
Javascript Question

Why did I get: `Uncaught SyntaxError: Unexpected token const`

I have created an empty

div
with the id of
warning_messages
and storing that div inside a
const
using javascript but it is throwing an error:
Uncaught SyntaxError: Unexpected token const


Here is the div code:

<div id="warning_messages"></div>


and the javascript:

const warningMessages = document.getElementById('warning_messages');


Using
var
instead of
const
also throws the same error:
Uncaught SyntaxError: Unexpected token var


Her is my complete JS:

// Initialize Firebase
var config = {
...
};
firebase.initializeApp(config);

//Handle Account Status
firebase.auth().onAuthStateChanged(user => {
if(user) {
console.log('Logged in');
} else {
console.log('Logged out');
}
});

//Handle Authentication
document.getElementsByTagName('button')[0].addEventListener('click', event => {
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;

firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;

switch(errorCode) {

const warningMessages = document.getElementById('warning_messages');
const warningMessagesContainer = document.createElement('p');
warningMessagesContainer.setAttribute('id', 'warningMessagesContainer');

case 'auth/wrong-password':
const invalidText = document.createTextNode('Invalid Password!');
break;

case 'auth/invalid-email':
const invalidText = document.createTextNode('Invalid Email!');
break;

warningMessagesContainer.appendChild(invalidText);
warningMessages.appendChild(warningMessagesContainer);
}
});

});

Answer

You cannot have anything aside from cases inside of a switch statement.

Your error is that you start to use a variable inside of a switch without a case.

Comments