Javascript no.1 - Data types / The let or not to var or const / Error handling 
Tags: javascript, beginners, tutorial
Published: 2020-07-21 17:29:54

This is the firsttt post about JS. I will try to be as concise I can be and give a "default" structure to every post, if you are looking for certain information you can go there easyly. 

### Concepts/things in this post
* _**Data types**_
* _**the diference between let / var / const**_
* _**error handling**_

### Data types

As a beginner in Javascript this are some of the most "common" data types that you are going to find and use:
```
* integrals / float - numbers
* string / char - letters or chained characters
* boolean / true or false / 0 or 1 / yes or no
* undefined / a value not placed or defined literally 
* null / nothing / is not there
```
When you are in JS the main situation is going to be how I define those data types and the answer is very easy. Use var/let/const to do it.
```
let number = 1 / integer
let pi = 3.14159265359 / float

let word = "hello" / String

let isReal = true
let isFake = false
```

As an idea to make understand the person learning JS one of the ways to check if the information I put in here is real is to check it with the console in your webbrowser. I use chrome, to access the combination of keys are *ctr+shift+i* and you can see a white/gray window in your web browser. Then in the top of that window you can see some options. Click in console and you will see this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g3wmng54qok6hh9rmdfo.jpg)

You can copy or writte in the console. The same variables I defined in the last pragraph and then do this:

`typeof *the value*`

typeof number, typeof pi, typeof word, typeof isReal, etc...

When you hit enter you will get some answers as "String","number","boolean".

Thats how you can see those values. 

Here is the tricky part, floats or numbers with point aside goes directly to the category of _"number"_ is not as specific you could believe but at the same thing JS will know what type of "number" it is. The same goes with String, you can define a character as "h" but will say _"String"_. For that we have some properties in js that we are going to learn in other posts. 

We have other two data types??? **Undefine** and **null**, Undefine is the absense of the specification of a value. 

`let a;`

If you put that in the console, you will get undefined. Js dont know what value holds, if is a number or a string an array. 

null is a little bit more complicated, because is nothing. It could be the representation of the void or space. It exists but exists as a representation of something that is not there. 

### var, let and const

This is a good thing to understand from the beginning because later we have to write functions and set boundaries. When you define a value you can use three ways to do it. But every way has a difference.
```
var a = 50
let a = 50
const a = 50
```
Imagine this, your birthday as a piece of information is known by you, your parents or dear people to you otherwise the whole world doesnt know your birthday. You can put _**let**_ in that part. 
Then we have national days, like the day of dead, historical dates, etc... in that enters _**var**_, all the country knows about that is like common knowledge. 
The final part is _**const**_ you can relate it with _**international days**_. Why? Because they are date that are already define. Like you will always know that the day of yoga is in Jun 21th and you can't change that.

In general
* **var** is a global value o globally scoped, they can be updated or redeclared
* **let** is blocked scope, can be updated
* **const** is blocked scope, they need to be defined since the beginning and can't be updated
```
var b = 10;
{
 const a = 40;
 let c = 139;
}
```

The excersice is the next:

Write in the console this things

```
var hello = 'hey im working';
let working = true;
if(working){
 let insidethebrackets = 'im working too';
}
console.log(hello);
console.log(insidethebrackets);
```

As you see, insidethebrackerts doesnt appear, even you get an error. That happens because inside of the if, insidethebrackets is working. It exist in that island as the only resident. Otherwise, hello is working because it appears outside. You can change console.log(hello) inside the if and hello will still work.

```
var hello = 'hey im working';
let working = true;
if(working){
 let insidethebrackets = 'im working too';
console.log(insidethebrackets);
}
console.log(hello);

```
Now it works.

Write this first
```
const number = 40;
```
Then
```
number = 10;
```
You get an error, its a constant and constants cant be updated. Most of the use of constants are as function declarations or as values that doesnt have to be changed. In one way you can put it like a formula or a well known info like the value of gravity or a byte in that sense.

### Error handling

When you write code or you want to understand what is happening, why im getting 2 instead of 2000000, we can use some things. We try to read the output of the value in the console. For that we use:

`console.log(*value*);`

What will do is print in the console the value as you saw in the last excersices. For what is this. Maybe the result is wrong, you dont know if you forgot to do an operation and maybe thats the problem, in the process of the declaracion the value could change and that made something to the operation. A lot of things can happen. 

```
var x = 1;
{
  var x = 2;
}
console.log(x);
```

Pretty much thats the only way to handle errors inside the code that are not as a proper error like "error reference", stuff like that. In that case, the other way to handle is google. Copy the error and look for something that resembles. 

### Offffftopic

Pretty much thats it. Im trying to not overload with information. I tried to make it as easy as can be. In the beginning of learning can be dificult, but with time will be as a second nature and give you the base for learning other stuff. Just keep trying and dont give up.

If theres something i need to change or it doesnt make sense, please write me or comment in the post!!