This is a basic javascript question. FYI the context is: It is in a Javascript controller for a Salesforce Lightning Web Component, editing in Visual Studio. The code is for selecting a set of table column definitions to render as a lightning-datatable. (I will want to be able to select which columns to display in order to reuse this for different reports.)

The following very short javascript controller code throws these errors. I removed all other code to simplify and make it reproducible, so this is the entire code in the file.

  • LWC1007: /Users/xxx/dev/xxx/xxx/xxx/MyClass/jsbugtest.js: Unexpected token, expected "," [12, 26]
  • ts(1005) ‘,’ expected. [12, 26]
  • ts(1005) ‘;’ expected. [12, 26]
  • ts(1128) Declaration or statement expected. [16, 1]

If I comment out lines 12-14 and uncomment line 10, the errors disappear and Salesforce accepts the upload.

I'd appreciate if you can tell me why the if statement does not work and where there is language documentation explaining it if possible. Thank you!

import {LightningElement, api, wire, track} from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class MyClass extends NavigationMixin(LightningElement) {
    generalReportName = 'general';
    complianceReportName = 'compliance';
    activeReportName = 'compliance';
    searchResultsColumns = [{label: 'Sales', fieldName: 'Record'}];
    complCols =            [{label: 'Compliance', fieldName: 'Record'}];
    //searchResultsColumns = (activeReportName == complianceReportName) ? searchResultsColumns : complCols; // line 10
    if (activeReportName != complianceReportName) { // line 12
        searchResultsColumns = complCols;
} // line 16
  • You don't not have any methods in the JS. The statements like if should be inside the methods. javascript.info/class – manjit5190 Oct 25 '20 at 10:55
  • @manjit5190 That should be an answer, not a comment. – sfdcfox Oct 25 '20 at 14:06

In all languages of which I am familiar, including JavaScript, executable statements are not allowed outside of a method. In both JavaScript and Apex, you'll find that this syntax is invalid:

// Apex Compilation Error example
public class X {
  Integer x;
  if(x == 5) {

// JavaScript Compilation Error example
export default class X {
  if(x == 5) {

You must put executable code in an executable block. In some languages, this is a static block, but in most languages, it is simply a function block.

The only thing you're allowed to do outside a method is to define variables and assign a value to those variables.

  • Thank you very much for your help. This explains why a ternary statement works but if doesn't. I was trying to quickly prototype before all of my methods and components were in place.. and was getting errors about properties not being defined yet. Perhaps connectedCallback() is a better place for it since I think properties are not available yet in constructor. My plan is to just set a default here and then change based on a widget component's event after rendering the default table format once. – skycafe Oct 26 '20 at 10:10

You don't not have any methods in the JS. The statements like if should be inside the methods.

Read more here: https://javascript.info/class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.