Understanding JavaScript as a Developer II

With the first article on JavaScript and the basics out of the way, let’s dive into other areas that are a bit more serious. I say serious but with understanding and practice, it will be a piece of work as you move forward. We’ll be considering these three concepts today:
- Data Structures
- Type Conversion
- Loops
Data Structures
Data structures are the techniques for the storing and organizing of data that make it easier to modify, navigate and access. Data structures are quite useful in the event where there are large databases and it also helps handle multiple requests from users.
There are 7 types of data structures in JavaScript and they include:
a. Arrays b. Linked list c. Stack d. Queue e. Tree f. Graphs g. Hash table
For the purpose of this lesson, we shall briefly discuss Arrays. Simply put, arrays allow you to store several pieces of data in one place.
This is an example:
var ourArray = [“Suya”, 67, 56];
An array has the features of what you see above:
- They start and end with a bracket
- Each element is separated with a comma
- Elements can be any data type
When one of the elements in an array is another array, it is called a nested array. How does this look like?
var myArray = [[“Janobest”, 20], [“Heaven”, 333]];
Your data is sitting pretty in your arrays, however there is the small problem of accessing them. How do you do this? Easy, you use bracket notation. Bracket notation is one way to access a property of an object. You can access data in the array with indexes. Here is an example:
var yourArray = [10, 20, 30]; var myArray = yourArray[1]; console.log(myArray);
The console will pull up the number 20. This is because in programming, we start counting from 0 not 1.
You can also use bracket notation to modify an array.
var ourArray = [18, 68, 99]; ourArray[0] = 15 console.log(ourArray); This will equal [15, 68, 99]
However, keep in mind that you cannot use bracket notation to modify a string.
What other ways can you modify an array? You can append data to the end of an array with the push function.
var ourArray = [“the city”, “love”] ourArray.push ([123]); console.log(ourArray);
You can also remove an item from an array with the pop function.
I would like us to take a quick detour and talk briefly about function. Functions allow us to create reusable code in JavaScript. This is how a function is set up. We have the word function then the function name and there’s always the parentheses which you can pass information into, and then the opening and closing brackets. Everything inside the curly brackets is run anytime the function is called or invoked. An example is given below:
function callBack () { console.log (“Hello World”), }
Everytime the function is called like this (callBack ();), the console is going to say “Hello World”.
TYPE CONVERSION
In JavaScript, type conversion is the process of converting data of one type to another. For example: converting String data to Number.
The two types of type conversion in JavaScript are:
- Implicit Conversion - automatic type conversion
- Explicit Conversion - manual type conversion
Implicit Conversion
In certain situations, JavaScript automatically converts one data type to another the way it is supposed to. This is known as implicit conversion.
result = ‘3’ + 2; console.log(result) The result equals “32”
This is because when a number is added to a string, JavaScript converts the number to a string.
Explicit Conversion
With explicit conversion, the source code explicitly requires a conversion to take place. The global method Number () converts string to numbers.
Number (“3.14”) returns 3.14
LOOPS
Loops are efficient for running the code over and over again with a different value. Loops are used in JavaScript to perform repeated tasks based on a condition. For instance, if you wanted to show a message 50 times, you can simply use a loop.
There are several kinds of loops in JavaScript but in today’s article, we are going to place emphasis on the for Loop. This is the most common type of loop in JavaScript. for loops are commonly used to run code a set number of times.
A for Loop looks like this:
for (let i = 0; i < 9; i++) { console.log(i); }
You start with the keyword for and there’s the parentheses with three different items in there which consists of:
a. Initialization b. Condition c. Final expression
Initialization The initialization happens before any of the code inside of the loop runs. As you can see above, i is initialized to be zero. This is what most for Loops start with
Condition This is checked each time before the loop runs. Conditions typically return true or false. A loop will continue running until the defined condition returns false. Once it is false, it breaks out of the loop.
Final expression At the end of each iteration, you increment i by 1 (i++).
The conclusion of this article would be to encourage everyone reading to do more research, look into the basics and write your own code. It can seem daunting but when you put in that extra effort to understand JavaScript, it becomes easier. Good luck!
