Click to See Complete Forum and Search --> : Best practices?


TenKracer
11-03-2003, 01:07 PM
When you declare a variable how can you declare it's data type? is it just figured out using the actual value? Also in a function what is the best way to declare a variable that is being passed in?

Jona
11-03-2003, 01:24 PM
Usually you don't have to, because you can treat a variable as any type of variable no matter what its value is (for example, treating it as a boolean value if its actual value is an empty string). However, you can use this method to set the data type, rather than using int, char, et cetera as you would do in something like C++.


var str = ""; // empty string
var num = 0; // "null" number
var bool = false; // false boolean variable
/* null is often treated the same as false, thus the variable num will evaluate to null or false, as will the variable str*/


To pass values to a function, you call the function like so:


<button onclick="alert_str('string');">Alert</button>


The function receiving the call should look like this:


function alert_str(str){
// str will equal whatever was sent to the function and be considered a string
alert(str);
}


[J]ona