This is the fourth article in a series provided for non-technical System Administrators who wish to understand more about using JavaScript to control workflow and to implement simple customizations using the language.
In this article, I will start to explain the different types of JavaScript data types and how we use them in Sage CRM scripts.
I discussed in the last article how JavaScript provides different data types to hold different types of values.
- Primitive data type
- Non-primitive (reference) data type
And I hope that you have started to understand that JavaScript is a dynamic-typed language. This means that you don't need to specify the type of the variable because it is dynamically used by JavaScript engine.
Our variables can hold any type of values such as numbers, strings etc. I want to look a little more closely at these.
Numbers
JavaScript makes no distinction between integers and floating point numbers. All numeric values use 64-bit floating point format
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);
This shows how numbers can be used in JavaScript.
The general rule in JavaScript is if it looks like a number then it will be treated like a number. But there is a different about how the two document.write() examples will work.
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.
Booleans
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.
Objects
Within JavaScript, an object is a compound data type.
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)");
We will be meeting objects and creating our own during the later articles. The next article will continue to explain the different types of JavaScript data types and how we use them in Sage CRM scripts. As part of that, we will look at the Browser Document Object Model and how objects are referenced and controlled in Client Side Coding.
An Introduction to JavaScript in Sage CRM
The links to the other articles in the series are listed below
- An Introduction to JavaScript in Sage CRM.
- The relationship that JavaScript has with other languages and where scripting can be used in practice.
- The syntax of the language and the different objects available.
- The different types of JavaScript data types and how we use them in Sage CRM scripts.
- The objects that are available within the Browser.
- Binding a script to an event.
- Statements in JavaScript.
- Conditional behaviour and compound statements.
- Another look at Strings, regular expressions and other objects.
- Arrays, Functions, and techniques for processing objects.