DEV Community

Cover image for Variables with Kitties and Memes
Edita Haroyan
Edita Haroyan

Posted on

Variables with Kitties and Memes

Hello fellow programmers and welcome to variables (please pronounce with italiano accent)
In this short and butiful blog we'll discuss what they are, what they do, and why they do what they do.
Everything I say is regarding JavaScript, so most probably it won't work for other programming languages

Let's start
First of all, what are variables?
The best desribtion would be to think of them as a box, in which I can put anything. And by anything I mean ANYTHING, starting with numbers and ending with "war and peace" (or a kitty)

Image description

In order to put something in the above mentioned box, first we write the type of the variable, then the name of the variable and what it contains, if it does. We can declare and assign them. Declaring means creating them, assigning means giving them a value which they will hold untill I change it. Here is the format we use

Image description

As you can see we assigned a variable to a variable, by doing that the first variable will get the value of the second one. And we can print them to make sure everything is correct (to print anything we use "console.log()").

Image description

There are three types of variables

1 . Var

Is used through the whole programm and can be changed or assigned.

Image description

2 . let

Is used only within the scope of where its declared. Meaning unlike var it can't be used everywhere.

Image description

3 . const

As you can guess from the name this one never changes

Image description

In our variables we can do basic math. Here are examples

1 . Addition

Image description

2 . Subtraction

Image description

3 . Multiplication

Image description

4 . Division

Image description

5 . Remainder

Image description

Some things to keep in mind

When naming a variable you can't use key terms (like var or function), and also they can't start with numbers, can't have space in them and most symbols won't work(only exception "_" and "$"). Therefore as much as I want to I can't name my variable
"69 & 420"

Now, this part is unnecessary if you only need varibles, but still pretty interesting and will be useful for the post about functions.
Arrays!!! They are like next level variables, so I thought it wouldn't hurt to talk about them.
Variables are boxes, however they are very small boxes because they can store only one value at a time. Array is a biiig box, so it can store more than one thing at a time (few kitties instead of one)

Image description

To make an array, we first make a variable then after "=" we put square breckets []. And whatever we want to put in the array, we put inside of it and separate with a comma. Here are some example

Image description

As you can see, we can put strings and numbers here. However we can also put an array inside, so it will look like this

Image description

This is called a nested array or a multidimensional array.
Now let's see what we can do with them. First of all, each element of an array has its index, which is basically their number (note that computers start counting from 0 so the first element will have an index of 0). To show the index of an array write the array's name and put [] these next to it (ourArray[]). When we want to see a particular element we can use indexes to print them. To do that we must make a new variable and assign them to the index of the element we want

Image description

As you can see, first we wanted to print the first element, so we used the index 0. Then we wanted to print the second element of the inside array, so we used two brackets, the first one meaning the number of the outside array and the second one being the index of the element from the small array.

That's all for now <3
Senk yu

Image description

Top comments (1)

Collapse
 
anetasargsyan profile image
anetasargsyan

Read the article for kittens only ♥