JavaScript : Variables, Types and Values

Every programming language is designed to manipulate values, these values are referred to as Types and the most fundamental characteristics of a programming language is the set of types it supports.  

When a programmer needs to store a value to use for manipulation they will assign it to a variable.

A variable is nothing more than a symbolic name for a value and allows the value to referred to by name.

JavaScript makes use of Variables, Types & Values extensively.

In this post I will attempt to provide enough information on how JavaScript Variables, Types and Values however I do recommend reading JavaScript : The Defenitive Guide,   JavaScript: The Good Parts and  JavaScript Patterns , these books provide a lot more in depth information than I will ever hope to cover in my tutorials. They are still my number one go to reference guides for JavaScript.

JavaScript: The Good Parts

Douglas Crockford identifies the abundance of good ideas that make JavaScript an outstanding object-oriented programming language

Buy

Object to Primitives

In JavaScript, Types can be divided into 2 categories : Primitive and Object types.

JavaScript’s primitive types include numbers, strings and booleans.  JavaScript also has special values of null and undefined but they are not numbers, strings or boolean.  

Any value that is not a number, string, boolean, null or undefined is considered an object.

An object is an un-ordered collection of properties where each property has a name and value.  

The JavaScript language also defines a special kind of object known as an array, that represents an ordered collection of numbered values.  

Arrays have some special behaviour that distinguish them from ordinary objects.

Another special kind of object in JavaScript is known as a function .

A function is an object that has executable code associated with it. Functions can be invoked to run executable code and return a computed value. 

Functions behave differently from other kinds of objects and JavaScript defines a special language syntax for working with them. 

The most important thing to remember about functions in JavaScript is that they are true values and that JavaScript programs can treat them like regular objects.

Variables

Before you can use a variable in JavaScript you should declare it.

You declare a variable using the var keyword. You should always use the var keyword to declare your variables in JavaScript because there are some really nasty side effects that can happen if you don’t!

JavaScript does not explicitly require you to use the var keyword to declare your variables, but if you don’t you’ll create what is called an implied global variable.

Variable Scope

A common cause of bugs in a JavaScript Application has to do with Variable Scoping, and peculiarities with how JavaScript deals with variables.

The scope of a variable is a region of  code in which it is defined.  However, variables declared within a function are defined only within the body of the function.




Creating and assigning a value to a variable without using the var keyword in a function will create an implied global variable and can be used outside the functions scope

Function Scope and Hoisting

JavaScript does not have block scope always declare your variables at the top of the function rather than declaring closer to the point at which they are used.

 

Contrary to some other C-based programming languages, JavaScript does not have block scope , JavaScript implements function scope which means variables are visible within the functions in which they are defined and within any functions that nested within that function.




function scope means that all variables declared within a function are visible throughout the body of the function. This actually means that variables are even visible before they are declared!  This feature of JavaScript is informally known as hoisting , which means that all variables declarations in a function are hoisted to the top of a function.




Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
π