My Web Development Blog Posts

JavaScript Language

Posted: 25/06/18

What are the use cases for the following JavaScript terms?

Parentheses:

  • They are used to declare the parameters/arguments for a function. These are values that a function takes in and uses within its code.
  • They are used when creating a "for" loop to contain the expressions of the loop.
  • They can be used to specify an expression to be evaluated ahead of the normal order of operations in the same way as you might use them in mathematics.

Brackets:

  • These can be used when you want to declare an array, a type of variable that can store multiple values.
  • They are also used when you want to access values from within an array by specifying the list order of the value you want to retrieve.
  • When you are wanting to access the properties of an "object" variable you could use brackets or dot notation. If you wanted to access a property with spaces in its name you have to use brackets to do this.

Braces:

  • These are used to wrap a block of code, this may be the code contained within a function, a loop, or within a conditional expression.
  • Braces are also used to contain the properties & values of a JavaScript object.

Single Quotes:

  • Single quotes can be used whenever you are wanting to declare a string in JavaScript. Single and double quotes can be used interchangably in this way but they cannot be mixed. If for example you start a string with single quotes you must also finish it with single quotes. If you wanted to use quotes within your string you could used double quotes within those however, e.g. 'example "text" string'.
  • Single quotes can be used when you want to pass some HTML code to a variable using JavaScript.

Double Quotes:

  • You would use double quotes in the same manner as I have described for single quotes above. You would also use them in conjunction with single quotes where you have a need to display a string within the string you have already declared, or where you need to exit the string in order to display some other code such as in some instances when you are using JavaScript to alter HTML elements of a website.