Broad Network


Primitive Values and Identifiers

ECMAScript Basics – Part 3

ECMAScript 6

Foreword: In this part of the series, we look at ECMAScript primitive values and identifiers

By: Chrysanthus Date Published: 12 May 2015

Introduction

This is part 3 of my series, ECMAScript Basics. ECMAScript has variables, similar to mathematical variables. In the strict sense, they do not behave like mathematical variables. In fact they are called identifiers. In this part of the series, we look at ECMAScript primitive values and identifiers. Values can be assigned to identifiers. The identifier name is the name you the website designer (programmer) gives.

Example
Consider the following statement:

    var myStr = "This is the third part";

Now, var is an ECMAScript reserved word. This means that you cannot use it arbitrarily. You use it only when you want to indicate that the next word is an identifier. In the above statement, the identifier is, myStr, meaning, my String. The value "This is the third part." is in quotes. It is a string. It is assigned to the identifier, myStr, using what is called the assignment operator, “=”.

Consider now the following two consecutive statements:

    var myStr = "This is the third part";
    document.write(myStr);

The first statement assigns the value, "This is the third part" to the identifier, myStr. The second statement writes (inserts) the value into the document (web page). The second statement has the, write() function. What you place inside the parentheses of a function is called the Argument of the function. What we have as argument here is the, myStr, identifier. This identifier holds the value, which will be written into the document. There are many situations in computing where you would use the identifier instead of the value. Read and try the following code:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<script type="text/ECMAScript">
    var myStr = "This is the third part";
    document.write(myStr);
</script>

</body>
</html>

The extra HTML code is to produce a web page.

Number as Value
You can use a number as a value. You can assign a number to an identifier. Consider the following two statements:

    var myNum = 56.48;
    alert(myNum);

The number, 56.48 is assigned to the identifier, myNum. When you assign a number, you do not have to put the number in quotes. The second line displays the number in an alert box. The alert function has as argument, myNum. Try the following code:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<script type="text/ECMAScript">
    var myNum = 56.48;
    alert(myNum);
</script>

</body>
</html>

Assigning an Identifier to another Identifier
You can assign an identifier to another identifier. Consider the following two statements:

    var str1 = "test";
    var str2 = str1;

The first statement assigns the string value, “test” to the identifier, str1. The second statement assigns the identifier, str1 to str2. The value of str2 is “test” copied from str1.

Changing the Value of an Identifier
You can assign a value to an identifier and then change it after that. Consider the following statements:

    var myStr = "test";
         myStr = "good";

The first statement assigns the value, “test” to the identifier, myStr. The second statement assigns a new value to the same identifier. The final value of, myStr is “good”. For the second statement, you do not need the reserved word, var, since you had already used it for the same identifier before.

Boolean Identifier
In some situations, you can have only one of two possible values. The value can either be, true or false. Any identifier that deals with these two values is referred to as a Boolean identifier. So, you can have something like:

    var myVar = true;

         or

    var myVar = false;

You do not need quotation marks around true or false.

Rule for Naming a Identifier
An identifier name must start with a letter or underscore, ‘_’. Within the name, you can have a letter, number or underscore.

Case Sensitivity in ECMAScript
ECMAScript is said to be case sensitive. This means that for identifier names, myVar is not the same as MyVar, which is not the same as myvar, which is not the same as MYVAR, etc.

Declaring an Identifier
Before you use an identifier, you should declare it. To declare an identifier, you begin with the reserved word, var, then a space, then the name of the identifier; just as you have seen above. You do not have to assign a value to an identifier when you declare it (assignment is optional at that point). You can do the assignment later. The following code illustrates this:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<script type="text/ECMAScript">
    var myVar;
        myVar = "me";
    alert(myVar);
</script>

</body>
</html>

In your code, for each identifier, the reserved word, var is used once. After using, var, if you need the identifier name again, you use the name without, var.

When to use the Reserved Word, var
Consider the statement:

    ident1  = 26;

This is an initialization statement. It is alright. In the statement, the identifier is given an initial value. If you want to declare a variable without initializing, then use the reserved word, var, as follows:

    var ident1;

You can use var and assign the value later as in the following code segment:

var ident1;
    ident1 = 26;

Consider the following two alternative statements:

    var ident = 42;

and

    ident = 42;

These two initialization statements are alright. Either of the statements can be used in place of the other. So, var is actually optional when you want to assign a value as you declare the identifier. Try the following code:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<script type="text/ECMAScript">

    myVar = "me";
    hisVar = "him";

    document.write(myVar);
    document.write("<br>");
    document.write(hisVar);
    
</script>

</body>
</html>

If you would not assign a value at the declaration stage, you have to use, var.

The undefined Value
When you declare an identifier without assigning a value to it, it means that the identifier is undefined. Actually the browser gives the value, undefined, to the identifier, unknown to you. Read and try the following code:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<script type="text/ECMAScript">

    var myVar;
    document.write(myVar);
    
</script>

</body>
</html>


The null Value
null means nothing. Nothing is the closest English word I know for the real technical meaning of null. I will not go into the technical meaning. ECMAScript has the value, null. It can be assigned to an identifier. Read and try the following code:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script type="text/ECMAScript">

    myVar = null;
    document.write(myVar);

</script>
</body>
</html>


When to use Quotation Marks for Values
If your value is a number or a Boolean value, undefined, or null, you do not have to use quotation marks. If your value is a string, you must use quotations marks.

ECMAScript Language Types
ECMAScript has two groups of language types: primitive data types and object types. I will talk about objects in a different part of the series. This part of the series is focused on primitive data types.

Primitive Data Types
Primitive types of the ECMAScript language are known as, String, Number, Boolean Undefined and Null.

The String Type
A string is text (value) in a pair of single quotes or in a pair of double quotes. Strings are of the string data type in ECMAScript. A string value is of the string data type.

The Number Type
An example of a whole number, also called an integer, is 53 . An integer does not have the fractional or decimal part. An example of what is called, a floating point number is 52.43; this is a number with a fractional or decimal part of .43 .  .43 is the decimal part of the number; it means the fraction, 43/100. Integer numbers and floating point numbers all form the number data type in ECMAScript. A number value is of the number data type.

The Boolean Type
The Boolean data type is a data type in ECMAScript, where the value is either, true or false. A Boolean value is of the Boolean data type.

The undefined Type
The undefined data type has as the only value, undefined. The meaning of undefined has been given above. The undefined value is of the undefined data type.

The Null Type
The Null data type has as the only value, null. The meaning of null has been given above. The null value is of the Null data type.

The Symbol Type
This is a new primitive data type - I will talk about it later.

The Primitive Values and Identifiers
Each primitive value of String, Number, Boolean, Undefined or Null can be assigned to an identifier in a statement. A string value has to be in quotes (single or double). The rest do not have to be in quotes.

Now, undefined means that an identifier (variable) has not yet been assigned a value; null, means nothing. undefined and null are reserved words in ECMASrript. Reserved words are words you do not use arbitrarily.

Literals
A literal is a value. Primitive values are literals. That is, String, Number, Boolean Undefined or Null value is a literal. Note, a string literal has to be in quotes (single or double).

Well, at this point, we have seen enough. Let us take a break here and continue in the next part of the series.

Chrys

Related Links

ECMAScript Basics
ECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message