Learning to Code: Day 36 — Basic JavaScript Part 1

Hugh Burgess
4 min readOct 24, 2020

Evening everyone.

I took a couple days from blogging to work on understanding WordPress and to some new aspects of computing, as I am trying to build a little ground in this area, I’m always keen to learn something new and a day without learning feels like a day wasted. As the title shows, we are heading into completey new territory. I have never laid a finger of thought on JavaScript so I’m going into this totally blind. Let’s see what FreeCodeCamp has in store for us tonight.

According to FreeCodeCamp’s introduction on JavaScript:

JavaScript is a high-level programming language that all modern web browsers support. It is also one of the core technologies of the web, along with HTML and CSS that you may have learned previously. This section will cover basic JavaScript programming concepts, which range from variables and arithmetic to objects and loops.

Commenting on JavaScript

When we write in this language, we aren’t using the same commenting techniques as HTML, JS uses two ways to comment:

  • The asterisk and forward slash for multi line coding:

/* hello here is a comment */

  • Using two forward slashes before text for in-line comments, which means to ignore the rest of the text on that line after the two forward slashes.

// Here is another comment

This is useful for other devs reading your code to understand what each chunk of your code is doing, and also useful for you in future referring back on something with a bit of context. I personally refer back alot so this will be helpful for that. Your welcome, future me.

Declaring Variables

This harks me back to Python, or what little I learnt of it on a phone app. Data is anything of value to the computer, and in JS, there are 8 total data types, which we’ll learn about as we go along:

underfined, null, boolean, string, symbol, bigint, number and object.

Computers can do mathematical equations with numbers, but not with strings, as strings are typed characters such as “Hello 123”.

A variable can be used to store and minipulate a data type, by stating it without directly using it. It is not a mathematical variable as it merely creates a value for a set of data at different times, and can be written with letters, numbers, an underscore (“_”) or a $ sign. But it cannot begin with a number or include spaces.

We can declare a variable in JS with the shorthand “var” preceding it and then state it’s custom label. For example:

var aName;

Note: The semi-colon ends the variable label.

Storing Variables with Assignment Operators

What is an assignment operator? Easy. It’s the equals sign (=).

If we want to assign a variable to a data type such as a number, we declare the variable as learnt above, and then define the variable on the left side of the assignment operator, and it’s corresponding number value on the right of the assignment operator. From then on, the code will represent that variable with that specific number value:

var number5;

number5 = 5;

Sounds about right doesn’t it?

Assigning the Value of One Variable to Another Variable

If we want to create a variable with the same value of another variable, we simply use the assignment operator to say this variable is equal to that variable, and the value of the two is then the same. We define one (line 1), give it a value (line 2), then define the other (line 3) and give it the value of the first one (line 4):

var var1;

var1 = 1;

var var2;

var2 = var1;

See how that worked?

Initialise Variables with the Assignment Operator

We can cut the above code above down by declaring and defining a variable in one line of code rather than over two lines. Don’t forget the semi-colon at the end:

var var1 = 1;

Boom.

Uninitialised Variables

When a variable is declared and brought into the land of the living, it’s initial value is undefined. Writing code with an undefined variable will result in NaN or “Not a Number”. Strings with undefined variables will return the string as “undefined”.

So, always remember to initialise your variables with a data type value!

Case Sensitivity in JavaScript

Variable labels like “variableOne” and “variableone” are not the same variable. Different variables can use the same words but in different casing.

It’s strongly recommended that you practise writing variables in JS with camelCase, which uses the first word of a multi-word variable in lowercase and the second word in the variable label beginning with a capital letter.

Aaaand let’s call it there for tonight, I think we’re out of the words on the basic basics of JavaScript typography. See you guys next time!

--

--