JavaScript Variables

The name of the variable: used when calling it.

Variables are what make up most of JavaScript. These variables make up things from numbers to objects, which are all over JavaScript to make one’s life much easier.

1. Defining a Variable

This variable is called a “string” because it has ASCII characters (A-Z, 0-9, !@#$, etc.)

var myVariable = "This is a variable!";

2. Using a Variable

var number1 = 5;
number1 = 3;

we defined a number called “number1” which was equal to 5. However, on the second line, we changed the value to 3. To show the value of a variable, we log it to the console or use window.alert():

console.log(number1); // 3 
window.alert(number1); // 3

To add, subtract, multiply, divide, etc., we do like so

number1 = number1 + 5; // 3 + 5 = 8 
number1 = number1 - 6; // 8 - 6 = 2 
var number2 = number1 * 10; // 2 (times) 10 = 20 
var number3 = number2 / number1; // 20 (divided by) 2 = 10

We can also add strings which will concatenate them, or put them together. For example:

var myString = "I am a " + "string!"; // "I am a string!" 

3. Types of Variables

var myInteger = 12; // 32-bit number (from -2,147,483,648 to 2,147,483,647)
 var myLong = 9310141419482; // 64-bit number (from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
var myFloat = 5.5; // 32-bit floating-point number (decimal)
var myDouble = 9310141419482.22; // 64-bit floating-point number
var myBoolean = true; // 1-bit true/false (0 or 1) 
var myBoolean2 = false;
var myNotANumber = NaN;
 var NaN_Example = 0/0; // NaN: Division by Zero is not possible
var notDefined; // undefined: we didn't define it to anything yet 
window.alert(aRandomVariable); // undefined
var myNull = null; // null // etc... 

4. Arrays and Objects

var myArray = []; // empty array

An array is a set of variables. For example:

var favoriteFruits = ["apple", "orange", "strawberry"];
var carsInParkingLot = ["Toyota", "Ferrari", "Lexus"];
var employees = ["Billy", "Bob", "Joe"]; 
var primeNumbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]; 
var randomVariables = [2, "any type works", undefined, null, true, 2.51];
myArray = ["zero", "one", "two"]; 
window.alert(myArray[0]); // 0 is the first element of an array                          // in this case, the value would be "zero" 
myArray = ["John Doe", "Billy"]; 
elementNumber = 1;
window.alert(myArray[elementNumber]); // Billy

An object is a group of values; unlike arrays, we can do something better than them

myObject = {}; 
john = {firstname: "John", lastname: "Doe", fullname: "John Doe"}; 
billy = {
    firstname: "Billy",    
    lastname: undefined,    
    fullname: "Billy"
}; 
window.alert(john.fullname); // John Doe 
window.alert(billy.firstname); // Billy

Rather than making an array [“John Doe”, “Billy”] and calling myArray[0], we can just call john.fullname and billy.fullname.

2 thoughts on “JavaScript Variables”

  1. I ⅼike the vaⅼuable info yoᥙ provide in your aгticles.
    I’ⅼl bookmаrk your weblog and check again here reguⅼarly.
    I’m quite sure I will learn a lot of new stuff right here!

    Good luck for the next!

    Reply

Leave a Comment

Skip to content