Javascript 8

Javascript 8

Chapter 8 with Examples

Hi everyone, My name is Arinze Calistus. The purpose of this blog is to teach you all you need to know about JavaScript.

Javascript Data Types

In JS or in any programming language, the concept of data types is important.** Data types** are of different kinds that can be stored or used in a Javascript program.

1. String Data Type

The string data type is a sequence of characters used to represent text. A string can be written with either single or double quotes.

var name = "John Doe"; // double quotes
var name1 = 'John Doe'; // single quotes
document.getElementById("demo").innerHTML = name + "<br>" + name1;

The question of whether to use double or single quotes is up to you. You can decide any, but nevertheless, if the string contains a single quote within, then you should use double-quotes. Or if it contains double quotes, use single quotes.

var sentence = "Her name is 'Jennifer'"; // make use of the double quotes in this case
var sentence1 =  'Her name is "Jennifer"'; // make use of the single quotes in this case

document.getElementById("demo").innerHTML =sentence + "<br>" + sentence1;

2. Number Data Type

The number data type is an integer or a floating point number (number with decimal).

var x = 4; // an integer
var y = 5.5; // a floating point
document.getElementById("demo").innerHTML = x + "<br>" + y;

Here is an example of adding two numbers:

var x = 4; // an integer
var y = 5.5; // a floating point
var sum = x + y ; // adds the two numbers
document.getElementById("demo").innerHTML = sum;

3. Boolean Data Types

The Boolean data type is a logical data type that can only have true or false as values.

var isCodingFun =true;
var isMathFun =true;
document.getElementById("demo").innerHTML = "Is coding fun? " + isCodingFun + "Is math fun?" + isMathFun;

Booleans are mainly used for conditional testing, not the one that we did in our previous example. Booleans are the output of comparison operators. The == comparison operator tests if two values are equal or the same. If they are, it returns true, otherwise false.

var x = 4; 
var y = 5.5; 
var areTheyEqual = (x == y) ;
document.getElementById("demo").innerHTML = areTheyEqual;

4. The undefined Data Type

If a variable has no assigned value, the value is undefined. In the example below, the title variable is declared but is not assigned to any value. Therefore, the value is undefined.

var title;
document.getElementById("demo").innerHTML = title;

5. The null Data Type

The null data type is a special data type denoting a null value -meaning it's an empty string " " or 0.

var fruit = null; 
document.write(fruit) ; // prints null

We can empty a variable by setting it to null.

var fruit = "apple"; 
document.write(fruit + "<br>") ;

fruit = null; // sets the value to null
document.write(fruit) ; // prints null

6. The Object Data Type

The object data type is a collection of related data. Objects contain properties written in key: value pairs. Each pair is separated by a comma (, ). Objects are written inside curly braces { }.

var banana = {
   color: "yellow",
    size: "long",
    quantity: 3,
    isSweet: true,
} ;

In the example above, the banana object has 4 properties: color, size, quantity, and isSweet.

Thanks for reading this blog. Do let me know what you think about it.