variables and Datatypes.
A Variable is like a container that holds a value (number, string, object..). The value of the variable can be changed(only using var or let, NOT const). To use a variable, you must first declare it like so;
We initialize it with “var” keyword (more on that in a bit), then a name usually what's in “the box” is the standard for naming variables and then an equal sign, with a value.
There are three different variables you can use:
- var = this variable is allowed to be used everywhere(or Globally), and it can be reassigned. (hardly used)
- Let = this variable is only allowed to be called in certain areas(or local scope) and it can be reassigned. (used sometimes)
- Const = this variable is only allowed to be called in certain areas(or local scope) and can NOT be reassigned. (used mostly)
Back to my box analogy; you put “objects” in a box. Then when you call on those boxes, they output the values in that box(If that makes sense…).
Why we use variables is so we can fill it with data and use it at a later time.
So, what's happening here?
We are setting two variables one to “myName” and another to “myLastName”. Then running console.log() (console.log is just a method to output information in your console on your browser). In between the parentheses, I put what we want to output, which is done with the “+” to “concatenate” two words (or join together.).
If we wanted to change the value of “myName” or “myLastName,” we would have to write the variable(without keyword var or let) and change the value with “=” and then the new value, like so;
We use “myName” and then a “=” sign to change the initial value to anything we want it to be.
Now if you call myName again it changed its value, easy peasy.
You can also use numbers “0–9”, lowercase “a-z,” uppercase “A-Z” in your variables names.
IT IS CASE SENSITIVE, so “myName” and “MyName” are two different things.
Try to name the variable something intuitive or something that describes what value it is holding.
THE DONT’S OF VARIABLES:
- Don't ever use underscores at the beginning of a variable name, like this “_button.”
- Don't ever put a number at the beginning of a variable.
NEXT, we talk about;
- Boolean it returns a “true” or “false” value
If you look down below, we set the variable to data, and the value of that is true. We then use an “if-else statement” if the first value is true then it will return “hurray it worked”. If it is false, it would go to the next part or “else” and run “nope!.” So since we set the data to equal to true, it returns that first console.log “hurray it worked”
I also switched the data value to false, which in turn skips over the first console.log since it is no longer true and it outputs “nope!”.
2. null means “no value.”
it's not going anywhere(DEAD INPUT, Not really though).
We use this sometimes, when you are writing code and you cant leave it blank statement.
You could also use null for math, because it outputs the same thing as 0;
3.undefined is very similar to null. Undefined means a variable has not been declared or has been declared but not giving a value.
Here we set a variable, but don't give it a value.
We try to add 2 to A, but we back “NaN” Which stands for “Not a number,” that's because it's undefined or we haven't set a value to that variable.
4. Number’s are pretty self-explanatory it's any number(look at the picture for examples)
one thing to mention, is that a number can be decimals,integers and whole numbers, as well.
5.String is just words, it's usually surrounded by quotations.
We set the variable “fun” to the value “bacon bits.” When we console.log it, we use the “+” symbol to concatenate it to the string “hello my name is ”.
Which outputs “hello my name is bacon bits”. Moving on…
6.symbol’s these are new to ES6, they have a unique identity and are immutable(value can not change).
With strings, if you had two variables that both had the same value of “Jonathan” they would return true. With symbols, they are unique and would return false, as it is set to have unique characteristics.
I have yet to use “Symbol()” yet in my coding adventure, but it's basically a way to make a very unique character string.
7.objects collection of properties. A property is an association between a key and value.
Here we are setting “myCar” variable to be a “new object()”
(basically making an empty object, with no characteristics.)
Then we are giving “myCar” some key elements and some values. By saying “myCar.make = “Ford” ”.(we are setting the key and the value at the same time)
Think of objects as Accordion Folder (myCar is what name is of the whole folder), then we have the sub-levels (The key names like make, model, doors, etc…) and then the values are what's actually inside those sub-folders(Ford, Mustang, 3, etc…).
You can also set the key and values like the photo above, and recommend it (probably the easiest way.)
check out some more….
Also we have React framework…
The Social Media…