Work Sample
Data Types in JavaScript
Two weeks ago, the SheCodeAfrica Mentorship program kicked off and thankfully, I was one of the selected mentees. Now know this, this was not my first rodeo. I had tried several times to pick up software/web development. However, each time I tried, I would get excited for the first two weeks, hit a roadblock, and then quit. Fast forward almost three years after my first attempt, I saw a tweet about a certain mentorship program for women in tech. Now seeing as I am a habitual course enrollee, I signed up for the program. I believed that what was lacking in my previous attempts was the sense of structure and professional mentorship this new program was going to offer me. This time I was determined to make it work. So far, I can say that I was right. Instead of trying to download so much information in so little time, the SCA mentorship program has provided me with a guideline as well as a mentor to reach out whenever I encounter any problems. Within the past two weeks, I have learned a few things, one of which I will discuss in this post.
What are Data Types?
If unlike me, you’re new to the concept of programming, then you may not know what data types are. However, if you’re going to understand JavaScript (or any programming language), this is a concept you must become familiar with.
Simply put, data types specify what forms of data can be stored and manipulated within a computer program. This means for you to write and run programs that work, they must be stored in the right form. In JavaScript, there are eight main data types. These data types can be further grouped into three main categories: primary/primitive data types (String, Number and Boolean), composite data types (Objects, Arrays, Functions) and special data types (null and undefined). With primary data types, you can only assign one value to them at a time. Any subsequent assignment will overwrite the initial value stored. However, with composite data types, multiple values can be assigned at the same time. Now let’s look at these data types in details.
String Data Types
String Data Types are used to store text. In order to create a string, simply enclose your text in double or single quotes.
e.g., let var b = “I love JavaScript”;
Whenever this variable is called, the text “I love JavaScript” is displayed.
Note: While you can enclose your string data in either single or double quotes, you must be consistent when writing your program. This means that you can not start a string with double quotes and end with single quotes and vice versa.
Number Data Types
This data type is used to store all positive and negative numbers. Unlike some other programming languages, JavaScript does not differentiate between integers or floats (decimal numbers). This means that
var x = 14; and
var pi = 3.14 are all stored as number data types.
In addition to all whole and decimal numbers, there are some special values that are also stored as a number data type e.g. Infinity and NaN (Not a Number). While infinity simply represents infinity as used in mathematics, NaN is returned as the result of an incorrect or invalid mathematical operation. E.g., if
Var a = 12;
Var b = “I am Nigerian”;
The operation, a + b will return NaN as var b is not a number data type thus rendering this operation invalid.
Boolean Data Types
The Boolean data types can only hold two values: true or false. They are useful in creating functions that rely on the results of conditional statements (If, If – Else, If – Else – If, switch – case statements etc.) to run. They are usually returned when comparisons are being made within a program.
e.g. if var a = 12, b = 10, c = 16;
the operation b > a will return false (which) is a Boolean data type.
Object Data Types
As mentioned earlier, Objects are composite data types that allow you to store collections of data properties. These properties are stored in key: value pairs. The property key is always a string, but the property value can be any data type. Below is an example of an object in JavaScript
Var dog = { “breed” : “German Shepherd”, “color” : “Black”, “age” = 12, “ears” = 2 }
Array Data Types
Arrays are used to store multiple values within a single variable. An array can contain multiple data types including other arrays.
To create an array, simply enclose the array elements in square brackets and separate them with commas e.g.
Var africanCountries = [“Algeria”, “Ghana”, “Eswatini”, “Liberia”]
Each value is assigned a unique numeric position (called an index) with which it can be called. Note that the indexing start from 0 and not 1. So if I wanted to extract the country Eswatini from this array, it would be specified as africanCountries[2] and not africanCountries[3].
Function Data Types
A function is an object that can be called to execute a block of code. For example,
Var introduction = function() {
return “Hi, My Name is Dorothy”;
}
This means that when the function “introduction” is called, it will return the value “My name is Dorothy”.
Undefined Data Type
This is a special data type that has only one value – undefined. This is usually returned when a variable which has not yet been assigned a value is declared.
Null Data Type
This is another special data type with the single value – null. This means that the variable that has been declared has no value. Note that this is different from the number data type zero, or an empty string “”. It simply means nothing. This data type can be used to empty an existing variable of all its contents. For example, if we declare a variable thus,
Var b = “Stone Age”, doing b = null; will empty the variable b, thus implying that b has no value.
Now that you understand the different data types in JavaScript, does this mean you are a programming guru? Not yet. This is just the beginning, but I’m sure you’re well on your way to being the brains behind some of the most innovative web applications we’re yet to see.