Programming Building Blocks

Some consider programming to be complicated, and I agree. But, every language and program tend to have basic building blocks. We’re going to go over these building blocks in JavaScript.

 

Basic Types

Every language has some basic types. These are the entities you interact with most often. These entities are often numbers(1, 2,3, 0.5, 2.3, etc) and strings (“Hello World”), Booleans (true, false), and Objects ({name:”Tim”}). Here’s an example of these building blocks:

When coding you will see these building blocks scattered around the program. They allow you to build complex programs with logic you’d use in the real world. Now, basic types are fine, but we need to be able to store them.

 

Variables

Variables are storage containers for basic types. By leveraging these containers, we can reuse our basic types in our programs. For example, we can reuse greeting multiple times around our program. Here’s an example:

As you can see we reuse the greeting in two different places for different effects. This reusability is why variables are so useful. But these alone don’t allow us to do anything substantial.

 

Loops

Loops are one of the most essential building blocks. They allow us to repeat a process multiple times in a program. This is a core element of many programs. But, what’s important is the fact that we are repeating a singular process multiple times; this means we should often start our program from the case of a single task. Here’s an example of a loop in JavaScript:

As you can see we use the loop to repeat the process of adding one to the start amount multiple times in order to raise the value up to ten. This is just a simple example, but it’s something you’ll use often to repeat a process across many values in a program. So, now that we’ve seen loops, let’s talk creating tasks.

 

Functions/Methods

Functions or methods are the encapsulation of blocks of code. They perform a single specific task or a combination of tasks. They are similar to variables because they emphasize reusing chunks of code. A good understanding of functions allows you to create more robust programs. They should not be underestimated. Here’s an example of a function:

As you can see in this function, we simply multiply whatever we put in by 2. In our case, 10 becomes 20. Now, we can write more complex functions that perform more robust jobs. This is why functions are so useful, they allow you to reuse tasks like the above multiple times in any way you like. Now, let’s talk about conditions and if else.

If Else

If else is one of the other fundamentals of programming. They give you options in executions. Essentially, they change the flow of a program given some logical condition. Here’s an example:

As you can see in this example we perform division given the number is equal to 2. Otherwise, we do multiplication. Also, notice we’re actually using Booleans; we’re checking a logical condition. If it evaluates to true or false, adjust the flow of the program.

These are a majority of the building blocks of programming. I hope understanding these improves your programming capability in the long term. Understanding that these building blocks exist across languages is important.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s