1

JavaScript is the lightweight dynamic scripting language that is popular for the web page development, however, it is also implemented for development in non-browser environments. It is an object oriented language that supports functional and prototype-based programming. This language offers great flexibility to developers as it doesn’t impose much conventions or guideline to adhere.

The amazing flexibility to implement the language in a desired fashion offers several benefits and one of its major advantages is that it doesn’t possess the block scope. It allows developers to define all the variables related to a function right at the beginning of the function. And, this approach is commonly known as a single var pattern.

Let’s have an insight into the topic and explore the implementation of single var statements and its effect.

How to implement single var pattern

2
In the Single Var Statement approach, all the variables are required to be included with only one var statement and separated by a comma. This var statement is required to be placed at the beginning of the function. And, to suspend the var statement, the last variable has to be terminated with a semicolon.

Code Snippet 1: for Single Var Statement
function myfunction() {
var x = 1000,
y = “What”,
z = { name: “Bond”, level: 1 },
a = new Date(),
b;
}
This code snippet 1 is absolutely equivalent to the below mentioned code snippet 2.

Code Snippet 2: for Multiple Var Statements
function myfunction() {
var x = 1000,
var y = “What”,
var z = { name: “Bond”, level: 1 },
var a = new Date(),
var b;
}

This simply showcases the difference between the two approaches of declaring variables in JavaScript. Although, there is no difference in the functionality of the two approaches, it is just that the latter one possesses a verbose solution.

Another way to implement the single var statement is that rather than placing the comma-separator at the end of each variable declaration, it can also be placed at the beginning of each variable declaration.

Code Snippet 3: for Single Var Statement (an alternative way)
function myfunction() {
var x = 1000,
, y = “What”,
, z = { name: “Bond”, level: 1 },
, a = new Date(),
, b;
}

I believe that comparatively this approach makes the code a little tough to read. However, it is absolutely a matter of individual’s preference, both the ways are acceptable.

No explicit data type declaration in JavaScript
3
Since, JavaScript is a dynamic language, data type of a variable is not required to be declared explicitly. All the data types are treated in the same fashion, so you can conveniently convert a variable from one data type into another without making any additional effort. Javascript supports a list of data types, including arrays, strings, undefined, and a lot more. This feature of JavaScript further supports Single Var pattern.

Also Read: 6 Tips for Becoming a Better JavaScript Developer

How Single Var Statement helps in beautifying the code

One of the upside of implementing Single Var Statement in JavaScript is that it makes the coding concise, as programmers need not write the “var” identifier again and again. Moreover, it also allows one to define and declare a variable at the same time. Let’s understand this benefit with an example.

Code Snippet 4:
// A function that returns a random integer number
// between two specified integer arguments.
var randomizer = function (a, b) {
var min = x,
max = y,
random = Math.random(),
result;

// Calculate random number
result = Math.floor(random * (max – min) + min);

return result;
}
// Display random number in an alert box
alert(randomizer(1, 100));

Code Snippet 5:
// A function that returns a random integer number
// between two specified integer arguments.
var randomizer = function (x, y) {
var min = x,
max = y,
random = Math.random(),
// Calculate random number
result = Math.floor(random * (max – min) + min);

return result;
}
// Display random number in an alert box
alert(randomizer(1, 100));

On comparing the Code Snippet 4 and 5 it can be observed that how one line of code is eliminated by simply defining and calculating for the result variable is done in a single var statement.

Also Check: 10 Free Books for Learning JavaScript

To conclude this, it can be said that it is completely up to personal preferences whether one wants to implement single var statements or multiple var statements in JavaScript programming. However, there is no harm in using single var pattern as they are quite simple to implement and rather reduces the amount of code.

Author Bio – Rick Brown is a highly skilled mobile app developer for Mobiers Ltd – a renowned iPhone app development company. In order to avail his assistance either to clear your queries or to avail detailed information, get in touch with him.