How to Declare Many Variables

Declaring multiple variables in JavaScript

The first way is easier to maintain. Each declaration is a single statement on a single line, so you can easily add, remove, and reorder the declarations.

With the second way, it is annoying to remove the first or last declaration because they start from the var keyword and finish with the semicolon respectively. Every time you add a new declaration, you have to replace the semicolon in the last old line with a comma.

How to declare multiple variables with specifying type using As in VBA?

The documentation you've linked to isn't wrong, but it's written for VB.NET and not VBA.

In VBA, as you've observed, any variable declarations that aren't immediately followed by As <type> will be Variant.

Therefore you'd need to write:

Dim a As Single, b As Single, c As Single, x As Double, y As Double, i As Integer

Shorter way to declare multiple variables in JavaScript?

"Faster"? You've determined there's a performance bottleneck here?!

In any case, you can declare multiple variables:

let foo    = 'foo'
, bar = 'bar'
, foobar = 'foobar'
;

But this is simply JS syntax–is this what you are really asking?

If you have a "large" number of related variables the problem may be more systemic, and there are multiple types of refactorings that might help.

Updated: I used to declare variables like this; I don't anymore.

Can you declare multiple variables at once in Go?

Yes, you can:

var a, b, c string
a = "foo"
fmt.Println(a)

You can do something sort of similar for inline assignment, but not quite as convenient:

a, b, c := 80, 80, 80

Declaring multiple TypeScript variables with the same type

There isn't any syntax that would accomplish this in a better way than just writing the type twice.

Initializing multiple variables to the same value in Java

String one, two, three;
one = two = three = "";

This should work with immutable objects. It doesn't make any sense for mutable objects for example:

Person firstPerson, secondPerson, thirdPerson;
firstPerson = secondPerson = thirdPerson = new Person();

All the variables would be pointing to the same instance. Probably what you would need in that case is:

Person firstPerson = new Person();
Person secondPerson = new Person();
Person thirdPerson = new Person();

Or better yet use an array or a Collection.



Related Topics



Leave a reply



Submit