+1 vote

[copied from GT FB group]


A general question about the curly bracket: { }.

When do you use it and when do you not use it?

For example:

>> challenges = [{challenge1}, {challenge2}, {challenge3}]

gives me errors.

BUT

>> association = {"{collection[1]}" -> 1, "{collection[2]}" -> 2}

doesn't give errors...

What is a simple way to remember when to use {} and when not to use it?

 

by (5.8k points)

1 Answer

+1 vote

The {} mean you “injecting” the value of a variable into some form of text.

So:

>> x = “Hi {name}!”

Means I’m injecting the value of name right into that text.

Similarly for:

>> x = {“dog”->”Dog”, “{myVariable}”->”Cat”}

I’m injecting myVariable inside that key. Keys (in associations) are always text.

Also:

Hello {name}!

Is a case where a variable is being injected into text that’s simply getting shown to the screen.

On the other hand:

>> animals = [variable1, variable2]

Doesn’t need {} because you’re not injecting variables into text, you’re just using their values directly.

by (5.8k points)
Welcome to Guidedtrack Q&A, where you can ask questions and receive answers from other members of the community.
134 questions
144 answers
55 comments
40 users