JAVASCRIPT

AN INTRODUCTION TO JAVASCRIPT DATA TYPES

learn javascript
Written by encodedBicoding
learn javascript
Today, we would be discussing Javascript and its data types. JavaScript is the behavioural language of the web. It is widely used to make web pages interactive.
To begin your study on how JavaScript works, make sure you have knowledge on how HTML and CSS works, because without these two prior knowledge, working with javaScript would be a very hard nut to crack. read the series How to become full stack in less than six months to get started on learning HTML and CSS.

In learning JavaScript, you need to know the different data type we use in holding/storing data.
Data-types helps us to store values into elements/variables, this help the computer processors to know how to process values in those elements/variable.
What is a variable?
A variable is simply anything that can help us store data, data that can be used in our program. In Javascript we need to declare variables before using them. Variables can be declared with 3 basic keywords or bindings.
    1. var
    2. let
    3. const.
so we can say…
var website_name = ‘encodedBicoding’;
let website_url = ‘https://encodedbicoding.com’;
const author_name = ‘Encodedbicoding’;
I must add that JavaScript is not a strongly typed language.
there are basically 2 types of JavaScript data types namely — Primitive and Reference data types
Primitive Data Types:
These are data types that are stored as simple data, which means they store there values  directly in their variables or bindings. Examples of primitive data types are:
    1. strings
    2. number
    3. Boolean
    4. null
    5. undefined
Reference Data Types:
These are data types that are stored as objects, which means they are only pointers to their value, referencing a location in memory. they do not store values directly in their variables, those variables serves as pointers. An Object is the “building block” of all reference data types. types of objects are:
    1. Arrays
    2. Function
    3. RegExp
    4. Date
    5. Object
    6. Error.
Primitive Data types are always created in their literal form, the below examples shows this form.

STRINGS

A string in javaScript represents anything written inside a single quote or double quote. A string data type is used to hold values like user name or even in objects(we would talk about objects in later lessons). A string can be represented like this:

 

var name = “encodedBicoding”;

The keyword ‘var’ in the above example is referred to as a binding, what it does is bind the value ‘encodedBicoding’ to the variable ‘name’. There are about 3 types of bindings in javaScript, namely: let, var, and const. We would go into all this in later lessons.

NUMBERS
Numbers in javaScript represents digits from 0-9.
var number = 12;
BOOLEAN

This is a type of data in javaScript that represents only two values: true and false.

var isWednesdayADay = true;

 

var isRobertoADay = false;
Reference data types can be created using their literal form or by their constructor form. the below examples distinguishes between their constructor format and literal formats.
ARRAY
This is an ordered list of values in which each value is stored in an indexed form.
The constructor format:
var my_array = new Array(1,2,3);
The literal format:
var my_array = [1,2,3];
if you console.log(my_array) it returns // [1,2,3].
while to get a value from my_array, we can achieve this by using the indexing capabilities of the array data type:
console.log(my_array[0]) // this returns 1;
the indexing of arrays starts from 0 not 1, this means that the value 1 is at index 0 of arr.
FUNCTION
Most developers use the literal format of the function object to create new functions, and this format is what I would use throughout this course.
to create a function literally, we start with the function keyword:
 
    function alertName(name){
         alert(name);
     }
while the constructor format could be written thus:

var alertName = new Function(“name”, “alert(name)”);

REGULAR EXPRESSIONS
Regular expressions are mostly used to check patterns in a value. This is really helpful when you want to validate a form field, say a user is supposed to fill in a string that has no symbols in them, you can use Regular Expression to check the user inputs like so:
Note that to begin declaring a regular expression literal, you use: / / (two back-strokes).
Inside the strokes can then contain your pattern checker.
To Check user inputs and not allowing any with symbols, one could write
let regEx = /(a-Z)/g;
if(regEx.test(userInput)){
console.log(‘Proceed’);
 } else {
console.log(‘Do not proceed’);
}

I would explain more on Regular expressions in an upcoming post.

OBJECTS
As a programmer, you will work a lot with objects. These can represent anything you want it to in life. Example. A car is an object, a game character is an object. A car has properties like tires, steering, doors and so on. Object data type helps you replicate real world objects in your code.
More on this in a post about object, links would be shared soon.
Let us define a Car object
let Car = {
   color: ‘blue’,
   mileAge: 100000,
   model: ‘honda Accord’,
   manufacturer: ‘Honda’,
  getModel: function(){console.log(this.model)},
};
If you notice from the above object, it’s properties consists of string data-type, number data-type and it also has a function. There are two ways properties of an object can be accessed:
1. Dot Notation.
2. Bracket Notation.
Dot Notation: To get the value of the car model, you can either use the function getModel, or use the model property like so: Car.getModel() or Car.model;
Bracket Notations: To get same values, we do: Car[‘model’] or Car[‘getModel’]();
This brings me to the end of this post for now, will be updated as soon as time permits me.

About the author

encodedBicoding

Software Engineer || Tech Enthusiast.
Ex @andela
Github: https://github.com/encodedBicoding
Geek Name: EncodedBicoding
Real Name: Dominic Isioma Olije

Leave a Comment