Brian's Blog

items I see across my tribes

JavaScript: Variables

June 25
by briancarter 25. June 2011 10:40

JavaScript is not a strongly typed language which means you rarely have to concern yourself with the type of data a variable is storing, only what the variable is storing and in JavaScript, variables can store anything, even functions.

Below are some samples.  Notice how isObject is an object so you can do things like - isObject.array[2]; isObject.color; isObject.dog;

<!DOCTYPE HTML>
<html>
<head>
  <title>Learning Javascript</title>
</head>
<body>
<p>JavaScript Variables 

<script>
	var thisIsAString = 'This is a string';
	var alsoAString = '25';
	var isANumber = 25;
	var isEqual = (alsoAString==isANumber); // This is true, they are both 25.
	var isEqual = (alsoAString===isANumber); // False one is a number, the other a string.
	var concat=alsoAString + isANumber; // concat is now 2525
	var addition=isANumber + isANumber; // addition is now 50
	var alsoANumber=3.05; // is equal to 3.05 (usually).
	var floatError=0.06+0.01; // is equal to 0.06999999999999999
	var anExponent=1.23e+3; // is equal to 1230
	var hexadecimal = 0xff; // is equal to 255.
	var octal = 0377; // is equal to 255.
	var isTrue = true; // This is a boolean, it can be true or false.
	var isFalse= false; // This is a boolean, it can be true or false
	var isArray = [0, 'one', 2, 3, '4', 5]; // This is an array.
	var four = isArray[4]; // assign a single array element to a variable.
	// in this case four = '4'
	var isObject = { 'color': 'blue', // This is a Javascript object
	'dog': 'bark',
	'array': [0,1,2,3,4,5],
	'myfunc': function () { alert('This is from isObject.myfunc'); }
	}
	var dog = isObject.dog; // dog now stores the string 'bark';
	isObject.myfunc(); // creates an alert box with the value "This is from isObject.myfunc"
	var someFunction = function() {
		return "I am a function!";
	}
	var alsoAFunction = someFunction; //No () so alsoAFunction becomes a function
	var result = alsoAFunction(); // alsoAFunction is executed here because ()
	// executes the function so result stores the
	// return value of the function which is
	// "I am a function!"		
</script>

</body>
</html>

LAMBDA

A final consideration on variables is that functions themselves can be defined like, and act like variables. Once a function has been defined it can be passed to other functions as an argument (A process knows as lambda), or assigned to other variables just like a string, array or any other JavaScript object.

Generally if you use a function without trailing parenthesis (), the function is treated like a variable and can be passed and assigned. Trailing parenthesis INVOKE the function, executing it and passing back the return value (if any).

Categories: Development


 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.