My favorite text-editor to program in is SublimeText, and you can get it here.
These guidelines will help you write clean, readable code.
- When naming variables, use camelCase, as in variableNameHere. Avoid underscores, like variable_name_here, and definitely don’t do variablenamehere.
- End each line with a semicolon
;even if it’s optional (see semicolon rules at the bottom of this page for exceptions).
- Separate tokens with spaces, like x + y = z, not x+y=z.
- Use // to comment code.
To assign the birthday to a value of your birthday, you use the = operator:
The Var Keyword
When you use the
The difference is the var keyword keeps a variable local, to a specific function, or in Meteor, to a specific file, if declared outside of a function. A rule of thumb with declaring variables in Meteor, is if you need it to be accessible throughout your entire application, in the global scope you leave the var keyword out. If you need localized to a specific function, you use it. This is a concept known as variable scope.
In the example above, we are declaring a function called
addThreeNumbers, telling it to accept three arguments, and to
return a value of the three added together.
This is known as a named function declaration. “Arguments” refers to
parameterThree. When you define the function, they’re known as parameters, and when you call functions, they’re referred to as arguments.
To call that function (think of this as asking your assistant to perform a task you taught him/her in the past), you simply do this:
Note: Function declarations do not end with a semicolon, and are one of the exceptions.
This will return a value of 18.
Function Expressions and Anonymous Functions
The difference between a function statement/declaration (can be used interchangeably) and a function expression is how the function is defined. Function expressions are typically anonymous functions (unnamed) which are assigned to a variable, or used within another function:
Here, addThreeNumbers is restructured so that it is assigned to a variable, and there’s an anonymous function within it, which logs a message to the console. It’s called in the same manner the initial function statement would be called.
The difference is it’s defined at run-time, instead of parse-time. What this means is if you were to call this function above the code, like so:
You’ll end up with an error. However, you can call a named function statement even before it’s been defined.
In the example above, we declare a variable called umbrella, and assign it to a Totes Umbrella. Then we declare a function called bringUmbrella, telling it to return that Totes Umbrella when it’s called.
Then we tell the browser to only call that bringUmbrella function, if the value of rainingOutside is true.
Else If & Else
If neither conditions are met (and you can use as many
else if statements as you want), it will use the
else statement as a fallback, and you can do something else there.
Also, if/else if/else statements do not end with a semi-colon.
Arrays are a great way to store multiple values that you want to group together. Arrays can contain anything a variable can contain: strings, booleans, numbers, objects, even other arrays. Below is an example array declaration, of strings.
You can also mix different types of values, like numbers with strings, within the same array.
Accessing Values in Arrays
Arrays use an index system to keep tabs of what value is in what position of an array. The index starts at 0. This means “Spaghetti” would be at index 0, and “Marinara Sauce” would be at index 1. In order to access a value within an array, you use this syntax:
You can read more about array operations here.
Accessing Object Keys & Values
In order to get the name of the thinkCodeReader, you use dot notation, like so:
The above example in Meteor would query a MongoDB collection called People, for a single document (person) with the name set to “John Smith.” Don’t worry too much about this right now, we’ll be discussing it much more later on.
toUpperCase() is called through dot notation on the
yourName variable. The return value of that method is re-assigned to the yourName variable in the second line, overwriting its initial value. So if you were to check it afterwards, yourName would be equal to “JOHN SMITH.”
Data Contexts & “This”
this is used to refer to the specific object you’re currently working on, accessing, iterating over, etc., etc. Basically
this is relative to its surroundings. Here is an example:
Here we’re using one of Meteor’s APIs, application programming interfaces, to get the value of the currently logged-in user’s id. We’re setting it to the variable named userId. Then we’re declaring a function statement, which looks to see if the value of
this.createdBy matches the currently logged in user’s id. If it does, it returns true, if not, it returns false.
this would refer to a MongoDB document (e.g. a post) with the field (key) of createdBy. This type of function could be used in Meteor to determine whether or not a user owns a post, to conditionally show them an edit button.
Throughout this tutorial, you’ve seen operators used multiple times, including the assignment operator and the addition operator. Below are a few more you’ll encounter while learning Meteor JS.
Above you see a single equals symbol, two equals symbols and three equals symbols used as operators. The single equals symbol is used to assign a value to a variable.
Double equals, the equality comparison operator, is used to check if two values are equal to each other, but does not look at the type, and will coerce strings to be equal to numbers.
Triple equals, the identity comparison operator, or “strict” equality operator is used to see if two values are identical.
String concatenation is demonstrated below, and simply uses the + operator to combine two strings together. Note the extra space at the end of the first string is there simply because concatenation would otherwise put them right next to each other.
The exclamation point
!== operator. The not operator will turn result in a boolean value, regardless of the original variable type, so a common practice is to do something like this:
What this does is convert the value of “value,” to a boolean value of “true,” after negating it twice, like a double-negative in English.
The above example utilizes the jQuery API to select every paragraph element in a document and add the class of “padded” to it.
Semicolons are obligatory when you have multiple statements on the same line.