JavaScript & Variables

Variables are the basis of any programing language, and are the backbone of any web-app ever. JavaScript has lots of different types of variables, each with their own advantages.



The Different Data Types

You can store different types of things in variables. One of the most used is a string, used to store text. This could be used to store a user's name. The string has to be surrounded by quotes. You can also store numbers, and booleans (True/False). There are also two datatypes that signify a variable is empty, these are null and undefined. These types are norally only used as outputs of functions, or when a user does not fill out a certain field.

var num = 14
var string = 'This is a string.'
var boolean = false
var name = undefined

Declaring basic variables

In modern JavaScript(ECMAscript 6), there are 3 ways to declare basic variables. These declarations use the key words: Let, Const and Var.

let var1 = 'Some text.'
const var2 = 735.13
var var3 = true

Whats the difference between them?

The keyword let creates a variable that can be changed throughout the script, while a const variable can't, as it is a constant. If you do attempt to reassign a constant, an error will show up in the console, and your program will stop. The keyword var, is almost the same thing as let, but variables defined with var will always be in the global scope.(see here)


Arrays

Arrays are huge in JavaScript, and are what make the language so powerful. They are very complex, and have many different functions that can be used on them (see here). This article will only scratch the surface of what arrays can do.


Creating Arrays

Arrays are created by declaring normal variables, but they can have multiple elements in them.

let myArray = [ 'element1','element2','element3']
//This array contains 3 string elements.

Arrays can have all type of datatypes. This includes: Booleans, Strings, and Integers.