Broad Network


Creating and Using Strings in ECMAScript 2015

The ECMAScript String Object Part 1

ECMAScript 6

Foreword: In this part of the series. I explain how to create and use strings in ECMAScript.

By: Chrysanthus Date Published: 14 Jul 2016

Introduction

This is part 1 of my series, The ECMAScript String Object. In this part of the series. I explain how to create and use strings in ECMAScript.

Pre-Knowledge
Every computer language builds up. You need to learn something today in the language, and then use it to learn something at a higher level in the same language, tomorrow. This series is part of my volume, ECMAScript Course. At the bottom of this page, you have links to the different series you should have studied before reaching here.

String Literal
Consider the following two statements:

        strA = "I love you.";
        strB = 'It is sweet.';

Each of the statements has a string literal. The first one is in double quotes and the second one is in single quotes. These are string literals and not string objects. A string literal is also called a string value.

The empty string is "" or ''. The empty string has no character even a space. A space is a character, so " " or ' ' is not the empty string.

String Object
The string object is an object like other ECMAScript objects with properties. For the string object, the literal is just like one of the properties. Remember, an ECMAScript object consists of properties, where some of the properties may be data properties and others may be methods. A property, which is a function, is called a method. A non-function property is a data property.

The Global Object
ECMAScript has a global object that is created by the browser and is executed before your script code is executed. You, the programmer does not see the global object. This global object has methods called constructors. One of these constructors is the String() constructor. This constructor is used to convert a value into a string or to create a string object, depending on how it is called.

When this constructor is called as a function, it converts a value into a string literal. When it is called as part of the new expression, it creates a new string object with its properties. The properties are automatically created as the string object is created.

Remember, the string constructor is also an object; it is a property and an object at the same time. It is a property of the global object. As a property of the global object, it is used without the preceding identifier and dot for the global object; that is a characteristic for the global object properties.

Converting a Type into a String Literal
You can convert a type such as a number into a string literal. To do this you call the String() constructor as a function. The syntax is:

    String ( [ value ] )

As you can see from the square brackets, value is optional. If value is not typed, the empty string, "" is returned. Read and try the following code, where a number is converted into a string literal:

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

    <script type="text/ECMAScript">

        str = String(25.36);

        alert(str);

    </script>

</body>
</html>

The output is:

    "25.36"

The extra HTML code is to produce a web page.

Creating a String Object
You create a string object when you call the String() constructor as part of the new expression, not as a function. The syntax is:

    new String ( [ value ] )

Here, value, which is optional is the string literal. The returned object can be assigned to an identifier. The return object has the string literal. The string object has been created with other properties automatically.  Read and try the following code:

    <script type="text/ECMAScript">

        strObj = new String ("I love you.");

        alert(strObj);

    </script>

The output is:

    "I love you."

If you are to display a string object such as strObj above, it is the literal that will be displayed.

The length Property
The length property returns the number of characters in the string literal of the string object. The syntax is:

    stringObject.length

Try the following code:

    <script type="text/ECMAScript">

        strObj = new String ("I love you.");
        
        len = strObj.length;

        alert(len);

    </script>

The output is:

    11

That is it for this part of the series. We stop here and continue in the next part.

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

NEXT

Comments

Become the Writer's Follower
Send the Writer a Message