Describe all the use cases for each of the following pieces of JavaScript syntax:

Parentheses: ( )

Parentheses generally denote a function, that is to say, set of repeatable instructions. If you wanted to write some code that prints 'Hello World' to the screen, you might make a function called helloWorld() which does this. That way, instead of writing the code out every time you wanted to print the line, you could just write helloWorld();.

But in that example, the parentheses don't do anything, they seem to serve no purpose. However, most functions accept arguments, which is data that the function acts upon. For example, imagine if instead of printing out 'Hello World', we instead wanted to say hello to a specific person. We might write a function called helloTo(). Then if we want to print 'Hello Harrison', we could write helloTo('Harrison') and it would print what we wanted. The information added in between the parentheses determines the arguments the function uses.

Parentheses are also used in a couple of other places in JS, if statements and loops. You can check my previous blogs to read more about what those are (although they are very DRY). In an if statement, parentheses are used to determine the condition the computer is checking. Lets say I want to tell the computer that if Cliff is hungry, he should eat lunch. The code I write might look something like:

                
                    if (cliff.hungry == true) {
                    eat(lunch, cliff)
                    }
                
            

In this case, the code between the parentheses on the first line is asking the computer to check if I'm hungry. The code on the second line is an example of a function, with two arguments: the first being what to eat, and the second, who is to eat it.

In the case of a loop, parentheses are used to determine how often the loop repeats. So if, flush with energy from eating lunch, I wanted to do 10 pressups, I might write a loop like so:

                
                    for (let i = 0; i <10 ;i++){
                        doAPressup(cliff);
                    }
                
            

Again, the code on the second line represents the instruction to be executed. On the first line, the code between the parentheses is JavaScript-speak for 'repeat this 10 times'.


Square Brackets: [ ]

Square brackets are most often used to denote arrays, which represent groupings of data. If I was to create an array for my shopping list in JavaScript, it might look like:

                
                    let shoppingList = [
                        'potatoes',
                        'butter',
                        'bread',
                        'rice',
                        '"The All Carbs Diet",
                        by Dr Jimmy McTerrible-Nutrition'                 
                    ]
                
            

The square brackets tell the computer the beginning and end of the array, and the commas separate each entry in the array. Information in arrays is sorted by its number, or 'index'. Square brackets are also used to extract information from an array according to its index. Indexes start at zero, which is sometimes confusing, but it means that if I want to get the second item in an array, I could access it by writing shoppingList[1]. We can also access properties of an object this way (see below).


Curly Braces: { }

We've already seen curly braces used in the examples above. They indicate blocks of code to executed in sequence. When you have a function, if statement or loop, the code you want it to execute is contained within the curly brackets that follow the parentheses.

Curly brackets also have a second function, which is to define objects. Objects are similar to arrays, but instead of storing information based on a numbered index, objects store data based on defined properties. For instance, if we wanted to make an object to represent a person, we might write:

                
                    let Cliff = {
                        first Name:'Cliff',
                        last Name:'Robinson',
                        age:31,
                        sex:'infrequent',
                        hungry:false,
                        pressupsdone:0
                    }
                
                

We can access these properties in two ways. The first is dot notation. Say we wanted to change the 'hungry' property of the Cliff object to true, we could write cliff.hungry = true;. But JS can only read properties this way if they have no spaces.

To access the properties with spaces in their name, we get to the second use of square brackets. If we were to change the property of the the Cliff object's first name to my full name, we would need to write cliff['first name'] = 'Clifford'.


Single Quotes: ' '
&
Double Quotes: " "

Both single and double quotes can be used to denote a 'string', which is a computing term for data represented as a series of letters. All the text on this page can be expressed as a string. Strings can be used as the names for properties of objects, which is why ['first name'] is in quotes in the paragraph above.

However, you often might want to have a string that has quotes within it. For instance, imagine you wanted a string which read: "Man, Cliff's blog sure is 'entertaining'."

If you tried to create it like so:

                
                    let aString = 'Man, Cliff's blog sure is 'entertaining'.''
                
            

The computer would interpret it as a a string reading 'Man, Cliff's blog sure is ', followed by some other text that doesn't make sense to it. However, if you changed the quotes around 'entertaining' to double quotes, it would read the whole string.


To index