- Primitive data type
- Non-primitive (reference) data type
Our variables can hold any type of values such as numbers, strings etc. I want to look a little more closely at these.
For example, these are all treated as the same data type
x = 1
y = 2.34
Z = .0001
If you try and treat something as if it were a number when it is not then you may get a special value returned 'NaN'
And it can be useful to know whether the variable in code contains a number so you can text for NaN using isNaN(). I'm not going to cover that here but you can find plenty of simple examples in the forums on this community.
Have a look at this code.
var x = 3;
var y = 2.5;
var zplus = x + y;
document.write("x plus y =" + zplus);
document.write("x plus y =" + x + y);
You can see that here the number in the first example has been calculated. In the statement zplus = x + y each was treated as a number and then tied to the string after the calculation was performed but the second shows that the 3 variables were just concatenated together – each was treated as a string.
A Boolean represents two values, either "true" or "false".
Many actions look for whether a true or a false is returned. Typically if something is returned that is not explicitly false then it is true.
This is another use of an implicit Boolean (true or false) check.
This example using a method ‘Values()’. It is the type of check that can be used in serverside scripts to access data passed from the browser to the server.
But here the code checks whether the case_slaid field – that is the service level field – has a value or not. If it does then it is ‘true’ and there is one set of actions to perform, and if it doesn’t return a value then it is false and the code does something else.
I will discuss if statements a little later in this series of articles as I want to explore conditional behaviour in more detail.
An object has properties and methods.
Objects are created with the new operator
var objTest = new Object();
And Typically use inbuilt objects based on existing classes
var myDate = new Date();
var myFunction = new Function("x", "y", "return(x+y)");