-6

Can i declare a variable like below?

var new_result_data_flag-- =1;

its giving error of ReferenceError: Invalid left-hand side in assignment.

  • 2
    `its giving error` Looks like you have your answer. – CertainPerformance Dec 21 '18 at 06:21
  • What is the purpose of that? What is the *actual* and *real* problem you need to solve? Please read about [the XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), as your question is an example of one. – Some programmer dude Dec 21 '18 at 06:25
  • No you canot , instead do like this var new_result_data_flag__ =1; console.log(new_result_data_flag__); – sradha Dec 21 '18 at 06:41

3 Answers3

0

It's very basic question in the programming world, 'how to create and initialize the variable'

You can do like

var new_result_data_flag = 10

// if you want to subtract value from the same variable then do as below

new_result_data_flag -= 1 // is equals to new_result_data_flag = new_result_data_flag - 1

console.log(new_result_data_flag) // 9
WasiF
  • 26,101
  • 16
  • 120
  • 128
  • my question is i want to add two minus symbol -- at the end of the variable. its giving me error Invalid left-hand side in assignment. FYI - i am talking about declaring a variable as per the given format. – Santosh Kumar Sahu Dec 21 '18 at 06:39
  • why do you want `--` at the end of the variable? what is the use case of that? Do you want to show it on the screen? – WasiF Dec 21 '18 at 06:41
  • @SantoshKumarSahu You can't use any arbitrary characters as part in your names, you have to follow the rules of the language. – Some programmer dude Dec 21 '18 at 06:51
  • I am calling a third party api which accepts the variable as per this format. – Santosh Kumar Sahu Dec 21 '18 at 07:11
0

Short answer: no.

Not only can you not declare variables as such, you shouldn’t even want to if it was possible. It would be confusing for the reader.

The double minus operators perform a post/pre decrement operation, i.e., subtracts one from the variable and returns the decrement value (pre-decrement ) or the original value (post-decrement).
Your statement attempts to decrement a non-initialized value which would be confusing at best. If you however simply need to have this naming scheme you could use an object property as such:

var bla = {};
bla['new_result_data_flag--'] = 'fluffy';
Ish
  • 71
  • 6
0

even you can use like this.

var new_result_data_flag_ =1;
console.log(new_result_data_flag_);

and my suggestion for variable declaration Names

  1. Names can contain letters, digits, underscores, and dollar signs.
  2. Names must begin with a letter
  3. Names can also begin with $ and _
  4. Names are case sensitive (y and Y are different variables)
  5. Reserved words (like JavaScript keywords) cannot be used as names