Forum Discussion

nish_b's avatar
5 years ago

Variable declaration in a javascript class



Can we declare global variables within a javascipt class, outside a function? When I try to declare a variable, it gives error: Syntax Error: Unexpected identifier

Sample code:

class Test {

var abc; //received above error

var abc = "Test"; //received above error

let def; //received above error

function1() {

//some code here

var abcd = "New"; //works fine here



Please suggest how I can create variables outside the functions in this scenario. Thanks!


2 Replies