Waqar Mohammad

Posted on

# Coding Bytes: Part 2 — Data Types, Operators & Syntax

This is part 2 in the Coding Bytes series, earlier parts are listed below:

## Data Types Continued...

In the first part we didn't get to finish of all of the data types. This was intentional as the post was getting a little longer than I wanted, and secondly, during the course of the series some things will be omitted so as to keep things as simple as possible.

Objects
We have covered `arrays` previously, and similar to arrays are `objects`. An example of an `object`:

``````var me = {
firstName:"Waqar",
lastName:"Mohammad",
age:33,
eyeColor:"brown"
};

``````

As you can see, `arrays` and `objects` are similar. As we move forward in the series we will go in detail about the difference between `arrays`, `multi-dimensional arrays` and `objects`. For now, we just need to know objects can be used to store data that has descriptive properties, such as in the example where I am the object in question. See the table below for a representation of the 'me' `object`.

Property Property Value
firstName Waqar
lastName Mohammad
Age 33
eyeColour brown

Boolean (again)
We covered that boolean is a `true` or `false` value which can also be represented with '1' or '0'. In actual fact there are a few more 'falsy' values. For now, we won't go into details but it's important that we skim over them.

##### Falsy Values
• `0`
• `false`
• `null`
• `undefined`
• `""`
• `NaN`

## Operators

Arithmetic Operators
`Operators` are the same as you will have seen in math(s) class during school and may use daily. These are known as `arithmetic operators`. The table below shows how they work.

Please note: `x = 10` and `y = 3` in examples.

Operator Symbol Example
Addition `+` x `+` y = 13
Subtraction `-` x `-` y = 7
Multiplication `*` x `*` y = 30
Division `/` x `/` y = 3.33
Modulas (remainder of a division) `%` x `%` 3 = 1
Increment `++` var z = x`++`. Result `z = 11`
Decrement `--` var z = x`--`. Result `z = 9`

The `modulas` operator may be a little confusing, but all we are doing in the example above is dividing `x` by 3 and asking the computer to carry on dividing equally as long as possible, then give us the remainder which here is 1. So instead of the `3.33` we get when we do the standard division of `10 / 3`, we are getting a remainder of `1` because `3` can be divided in to 10 a total 3 times, but on the fourth attempt, there is only `1` left - which is what the `modulas operator` gives us. I know it can be confusing at first 😕! But you will get used to it, I promise.

Another thing to note is that the `++` and `--` operators can come before or after the value in question e.g. `++y` or `y++`, and the positioning is important which we will review at a later stage.

Lastly, there are other operators which we will review as the series proceeds.

## Syntax

`Syntax` in Javascript (and other languages) are a set of rules, such as we have in spoken languages like English.

Keywords
We have come across the `var` keyword before. Keywords are reserved for certain actions where `var` is used to assign a variable. It must be noted `VAR` or `Var` cannot be used instead of `var` as Javascript is case sensitive.

Semicolon
A semicolon or `;` is used to end a statement, like a full-stop or period `.` would end a sentence.

Comments
We will often need to comment in code, so starting with two slashes `//` will let ensure any text/data after them will be ignored. An example is shown below. Note the multiline comment syntax too.

``````// this is an example of a comment.
var test = "test";

/*
This is a
multiline comment
*/

``````

Identifiers
`Identifiers` are essentially names. The rule in Javascript is that a first character of an identifier must start with a `letter`, `_`, or a `\$` sign. It cannot start with a `number`. A fun way of learning / testing if your identifier is valid can be seen here.

Types of Case

##### Camel Case

In programming we will come across a few types of case. We have come across `camel case` in the `object` example above where `firstName` was the `property`. In `camel case` the words have no space between them and the first letter of each word, excluding the first word, are capitalised.

##### Pascal Case

`Pascal case` is exactly like `camel case`, the only difference being the first word has a capital letter too. So instead of `firstName` as it would be in `camel case`, here it would be `FirstName`.

##### Others

Other examples are `underscore` where each word is separated by an underscore, `first_name`. And `hyphen`, `first-name`.

Whitespace and Line Breaks
Javascript ignores any spaces that you put in between code, unless they are specified in something like a `string`.

Thanks for reading. If you want to join along in my coding journey come say hi 👋 on twitter. I can be found @lawyerscode