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 [amazon text=JavaScript : The Defenitive Guide&asin=0596805527],   [amazon text=JavaScript: The Good Parts &asin= 0596517742] and  [amazon text=JavaScript Patterns&asin= 0596806752] , 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


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.


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
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.

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.

Buy me a coffeeBuy me a coffee