17 Contributors: 1 Tuesday, July 18, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial:
Roadmap: roadmap

JavaScript Variables


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.


  • var {variable_name} [= {value}];


variable_name{Required} The name of the variable: used when calling it.
=[Optional] Assignment (defining the variable)
value{Required when using Assignment} The value of a variable [default: undefined]


"use strict";

'use strict';

Strict Mode makes JavaScript stricter to assure you the best habits. For example, assigning a variable:

"use strict"; // or 'use strict';
var syntax101 = "var is used when assigning a variable.";
uhOh = "This is an error!";

uhOh is supposed to be defined using var. Strict Mode, being on, shows an error (in the Console, it doesn't care). Use this to generate good habits on defining variables.

You may use Nested Arrays and Objects some time. They are sometimes useful, and they're also fun to work with. Here is how they work:

Nested Arrays

var myArray = [ "The following is an array", ["I'm an array"] ];

console.log(myArray[1]); // (1) ["I'm an array"]
console.log(myArray[1][0]); // "I'm an array"

var myGraph = [ [0, 0], [5, 10], [3, 12] ]; // useful nested array

console.log(myGraph[0]); // [0, 0]
console.log(myGraph[1][1]); // 10

Nested Objects

var myObject = {
    firstObject: {
        myVariable: "This is the first object"
    secondObject: {
        myVariable: "This is the second object"

console.log(myObject.firstObject.myVariable); // This is the first object.
console.log(myObject.secondObject); // myVariable: "This is the second object"

var people = {
    john: {
        name: {
            first: "John",
            last: "Doe",
            full: "John Doe"
        knownFor: "placeholder names"
    bill: {
        name: {
            first: "Bill",
            last: "Gates",
            full: "Bill Gates"
        knownFor: "wealth"

console.log(; // John
console.log(; // John Doe
console.log(people.bill.knownFor); // wealth
console.log(; // Gates
console.log(; // Bill Gates

Related Examples