Open In App

JS++ | Variables and Data Types

Improve
Improve
Like Article
Like
Save
Share
Report

In this tutorial, we will introduce variables in JS++. Let’s start with an example. Create a new folder and name it “Variables”. Then create a new file and name it “Variables.jspp”. Write in the following code:

external $;
    
string firstString = "This is a string.";
int firstInt = 1;
double firstDouble = 1.1;
bool firstBool = true;

$("#string_content").text(firstString);
$("#int_content").text(firstInt);
$("#double_content").text(firstDouble);
$("#bool_content").text(firstBool);

Save Variables.jspp to your Variables folder. Then create a second file named “Variables.html” and write in the following:

<!DOCTYPE html>
<title>Variables program</title>
<body>

<p id="string_content"></p>
<p id="int_content"></p>
<p id="double_content"></p>
<p id="bool_content"></p>

<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="Variables.jspp.js"></script>

</body>
</html>

Save Variables.html to your Variables folder. Compile Variables.jspp and then open Variables.html in a browser. If everything has worked, your document should display the values of the four variables in Variables.jspp.

  • Variables and types

    When we declared our variables in Variables.jspp, we gave each of them a type: string, int, double, and bool. The fact you can use such typed variables is an important feature of JS++, and represents a key difference with JavaScript. In JavaScript, you declare a variable with the var keyword and such variables are not typed. The same variable might first hold a string, for example, then later a number, and then later a boolean value. In JS++, by contrast, when you declare a variable as belonging to a given type, you tell the compiler that the variable will hold only data of that particular type. So for example, string variables hold sequences of characters, int variables hold integer numbers, double variables hold floating-point numbers, bool variables hold logical values (true and false), and so forth.

    What happens if your program accidentally assigns the wrong kind of value to a typed variable? This results in compilation failure, not a runtime error. For example, look again at the line:

    bool firstBool = true;

    Suppose you change this to:

    bool firstBool = "This is a string, not a bool.";
    

    Your program will no longer compile. If you try, the compiler will complain that it cannot convert a string to a bool.

    Why is it better for a problem of this kind to cause a compilation failure, rather than a runtime error? The answer is that it makes the bug easier to spot and easier to fix at an earlier stage of development. It becomes less likely that the bug will remain hidden away in some complex part of your code, only to emerge at a crucial stage after your program has gone live. This can be especially useful in large projects and teams. In this respect, JS++ offers an advantage over JavaScript, which does not bring errors of this sort to light at compile time since it does not use typed variables.

    Note: JS++ does not force you to use typed variables. You can still use untyped variables if you wish, just as you would do in JavaScript – more on this in Chapter 9. When you do use typed variables, however, their values are guaranteed to be of the right type if the program compiles.

    The four types we used in Variables.jspp – string, int, double, and bool – are among the primitive data types of JS++.
    You can find a full list of them here: JS++ primitive data types

    Regarding the numeric primitive data types, it’s worth noting that JS++ contains not only int and double but also byte, short, unsigned int, and unsigned short (the unsigned types can only contain positive numbers). These types vary both in the amount of memory that they use and in the range of numbers that they can hold.

    Regarding the textual data types, JS++ contains char as well as string: in a case where you know that a variable should hold a single individual character, it would be appropriate to declare your variable as a char. More generally, the wide variety of primitive data types in JS++ gives you a great deal of flexibility in selecting the most appropriate variable type for your purposes.

    JS++ data types are not limited to its primitives. For example, there are also function types, array types, callback types, and user-defined types. We’ll examine all of these in subsequent tutorials. In this tutorial, however, the focus will remain on primitive types.

  • Declaring and initializing variables

    In Variables.jspp, we declared the four variables in separate statements, but we initialized each variable in the same state as we declared it. You don’t have to introduce variables like that, however. For example, you can declare a variable in one statement and then initialize it later:

    bool firstBool;
    // ...
    firstBool = true;
    

    You can also declare several variables of the same type in a single statement:

    char firstChar, secondChar, thirdChar;

    And you can initialize several variables of the same type when you declare them:

    char firstChar = `a`, secondChar = `b`, thirdChar = `c`;

    What happens if a variable is accessed after it has been declared but before it has been initialized? To see, let’s change the code of Variables.jspp:

    external $;
        
    string firstString;
    int firstInt;
    double firstDouble;
    bool firstBool;
    
    $("#string_content").text(firstString);
    $("#int_content").text(firstInt);
    $("#double_content").text(firstDouble);
    $("#bool_content").text(firstBool);

    If you compile this code and then open Variables.html in a browser, you’ll see that each of the variables is set to a default value: firstString is set to “” (empty string), firstInt and firstDouble are set to 0, while firstBool is set to false. You can see a full list of the default values for JS++ primitive data types via the link above.

  • Variables and scope

    The scope of a variable is the part of the program where that variable is visible (accessible by name). In JS++, a variable’s scope is determined by the code block (a section of code within a pair of curly braces) where it is declared: the variable becomes visible immediately after declaration from any subsequent place in the same block. For example, consider:

    {
        string firstString;
        firstString = "Another string.";
    }

    Here there is no problem, since the value of firstString is modified after firstString has been declared but within the same block. By contrast, consider:

    {
        string firstString;
    }
    
    firstString = "Another string.";

    Now there is a problem: this code won’t compile because firstString is not visible outside the block where it is declared.

    How does scope work when a variable is declared outside every explicit code block (i.e. every block defined by a pair of curly braces)? In this case, the variable is visible from anywhere in the file after it has been declared. This also applies to cases where a file doesn’t contain any explicit code blocks, as in Variables.jspp.


Last Updated : 01 Aug, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads