Global Variables in JavaScript

This is a simple thing to remember, but quite useful.

To define just a normal variable in javaScript you simply write the following:

[js]
var myVar = ‘foo’;
[/js]

To define a global variable simple loose the var in front.

[js]
myVar = ‘foo’;
[/js]

Pass a String as an Integer in JavaScript

I have been doing a lot of JavaScript recently, a language which is not as forgiving as PHP.

I was trying to pass what I thought was an integer to a function that only accepted integers.

[js]
$(‘#slideshow’).cycle(slideId);
[/js]

slideId had been defined in a tag and retrieved by using the following.

[js]
var slideId = $(this).attr(‘page’);
[/js]

slideId only contained a number but still cycle() throw an error. You cant seem to define a var as int instead the following code fixed the issue.

[js]
var slideId = $(this).attr(‘page’);
$(‘#slideshow’).cycle(parseInt(slideId));
[/js]