# Variable declaration in a javascript class

**URL:** <https://community.smartbear.com/t/variable-declaration-in-a-javascript-class/313>\
**Category:** TestComplete\
**Tags:** test, testcomplete, migrated, data-driven-testing, scripting\
**Created:** [July 18, 2019, 9:07am UTC](https://community.smartbear.com/t/variable-declaration-in-a-javascript-class/313 "2019-07-18T09:07:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [July 18, 2019, 9:07am UTC](https://community.smartbear.com/t/variable-declaration-in-a-javascript-class/313/1 "2019-07-18T09:07:30Z")

</div>

> Originally posted by **nish\_b** on July 18, 2019

Hi,

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!

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [July 18, 2019, 12:33pm UTC](https://community.smartbear.com/t/variable-declaration-in-a-javascript-class/313/2 "2019-07-18T12:33:43Z")

</div>

> Originally posted by **tristaanogre** on July 18, 2019

This syntax is not allowed in ES6 JavaScript.

[https://stackoverflow.com/questions/22528967/es6-class-variable-alternatives](https://stackoverflow.com/questions/22528967/es6-class-variable-alternatives)

[https://stackoverflow.com/questions/41212099/making-a-es6-variable-global-in-a-class](https://stackoverflow.com/questions/41212099/making-a-es6-variable-global-in-a-class)

This is not a TestComplete restriction but a restriction in the JavaScript language itself. To declare a variable within a class, it needs to be a property of the class or, as you did so, scoped within a method in the class. It’s all about scoping and variables are not supported in the scope definition of a class.

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [July 19, 2019, 11:45am UTC](https://community.smartbear.com/t/variable-declaration-in-a-javascript-class/313/3 "2019-07-19T11:45:54Z")

</div>

> Originally posted by **nish\_b** on July 19, 2019

Thanks for your response.

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [October 7, 2026, 5:15pm UTC](https://community.smartbear.com/t/variable-declaration-in-a-javascript-class/313/4 "2026-10-07T17:15:48Z")

</div>

Closed on October 7, 2026, 5:15pm UTC

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
