Lets get this data!: Looking into JavaScript data types
Tags: javascript, beginners
Published: 2023-08-17 18:27:40

What's a ghost's favorite data type? BOOlean! 

![Ghost](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qh25jvze3t8atm61ixg.gif)

Data types are a key concept in all programming languages but today we will examine some important JavaScript data types. 

- Number

The number data type represents integers or floating point numbers formatted to use a double-precision 64-bit binary format IEEE 754 value.
{% embed https://en.wikipedia.org/wiki/Double-precision_floating-point_format %}

```
let age = 37; 
console.log(typeof age); => number
```

![Fred counting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8j32tgdu29hr9x09rdzi.gif)

- String

The string data type represents a series of characters within a single ('') or double ("") quotation marks. Strings can be created as primitive values or objects using the new String() constructor. 
{% embed https://www.w3schools.com/js/js_strings.asp %}

```
let str = "Hello World!";
console.log(typeof str); => string

let num = 4;
console.log(typeof num); => number
num = String(num);
console.log(typeof num); => string
num = new String(num);
console.log(typeof num); => object

//Be careful not to use single ('') quotes when you 
//are including an apostrophe in your string 

let strApo = 'This can't work'; => SyntaxError: Unexpected identifier

let strApo = "This will fix the problem's";

```

![string](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/017ab3r4pnakxt0ng2ab.gif)


- Boolean

The Boolean data type can represent two values: true or false. This is the basis for all JavaScript comparison operations and conditional test. 
{% embed https://www.w3schools.com/js/js_booleans.asp %}
```
let booVal = true;
console.log(typeof booVal); => boolean

let numOne = 4;
let numTwo = 6;

let greatThan = numOne > numTwo;
console.log(greatThan); => false

let val = true;

if(val){
console.log("this is true");
} else {
console.log("this is false");
}; => "this is true"
```


![true](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y01yt4gtg2x5rr1cdpa.jpeg)

- Array

The Array data type is used to store multiple data values within a single variable which can be accessed from an index starting at 0 and separated by square brackets []. They can hold different data types including functions. 

{% embed https://www.w3schools.com/js/js_arrays.asp %}

```
          [0]    [1]     [2]         [3]                       [4]
let arr = [14 , "hello", true, function(a , b) { return a * b}, [3 , 4]];

//when type of is ran arrays will always show up as objects
console.log(typeof arr); => object

console.log(arr[0]); => 14

console.log(arr[3](3, 4)); => 12

console.log(arr[4][0]); => 3

 
```

![array](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79lbsvzz36bhrii34kwf.jpg)

- Object

The Object data type represents a collection of key-value pairs with curly brackets {}, where each key has an identifier and corresponding value of any data type. 

{% embed https://www.w3schools.com/js/js_objects.asp %}

```
let obj = {
  firstName: 'Alex',
  lastName: 'Beasley',
  inSchool: true,
  fullName: function(){
    return this.firstName + " " + this.lastName;
  }
};

console.log(typeof obj); => object

console.log(obj.fullName()); => Alex Beasley

console.log(obj.inSchool); => true

console.log(obj['firstName']); => Alex

```

- Undefined, null and NaN

Undefined is a data type which is a variable that has been declared but has not been assigned a specific value yet. It can also represent a function return that doesn't actually return anything. Null is the intentional absence of any object value. NaN (Not a Number) is a value that is considered not a valid number. 

```
let testVar; 
console.log(testVar); => undefined
console.log(typeof testVar); => undefined

function find(array, value){
 for(let i = 0; i < array.length; i++){
    if(array[i] === value){
      return array[i];
    }
  }
return null; // if value is not found return null
}

let array = [1, 2, 3, 4]
console.log(find(array, 5)); => null

let result = 10 / 'Alex';
console.log(result); => NaN

```

![Corgi](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/96ofspysftbwgg3n5ghc.gif)

{% embed https://www.w3schools.com/js/js_datatypes.asp %}

Date types, whether privative or complex, help software devs store different kinds of information. By understanding and using JavaScripts built in data types, it will make our lives as developers much easier.