### Data Types

In the previous lesson we learned that we can use variables to store values. These values can be of different types. The most basic ones are Strings, which are text values like "John", "I'm 34 years old" or "www.google.com" and Numbers.

These two types plus the Booleans (true or false) and Null and Undefined, are know as the primitive data types. Besides the primitives we also have two important data types which are the Arrays and Objects. We are going to explore each of these data types in details.

To find out what's the type of a variable we can use the typeof operator:

        var firstName = "John";

        console.log( typeof firstName ); // the result will be "string"

        var age = 40;

        console.log( typeof age ); // the result will be "number"

### Strings

Text values are called strings. Actually the best definition for a string is a sequence of characters. Strings are identified with single or double quotation marks.

Let's create some example variables to store string values:

        var firstName = "John";  // we can use double quotation marks

        var lastName = 'Doe'; // or single quotation marks

        var passportNumber = 'XYZ0123';

        var phoneNumber = "909299999";

        var email = 'john_doe@gmail.com';

        var bio = "John Doe started his web developer career in 2010 and has contributed to many important projects, like...";

Note that in the examples above we have the phoneNumber value composed by numbers only. Why is it string and not number type?

Although the value is composed by numbers only, it won't be used in any calculations so the string type is more indicated.

On the other hand if we are to store the year of birth of a person we might as well use the number type, because we can subtract the current year by the year of birth to find the person's age.

There are important operations that can be done with strings, as shown below:

- ##### Concatenation

        var firstName = "John";

        var lastName = "Doe";

        var fullName = firstName + " " + lastName;

        console.log(fullName);

        // The result will be "John Doe"

        // Therefore the plus sign (+) is used for concatenation with string values.

- ##### Length and Index

As said above, strings are sequences of characters. Those sequences has a size (length) and each of the characters has their specific position in the sequence, which is the index.

        var firstName = "John";

        console.log(firstName.length); // The result will be 4

        /*
        The length property of a string represents its number of characters.
        Each character has its own index, starting from zero.
        White spaces also count.
        */

        console.log(firstName[0]); // The result will be 'J'

        // We can access each character of a string using their index inside brackets.

- ##### String replace

There are useful ways of using the replace method. Let's say we have the following url: "https://www.udemy.com". We can easily remove the https:// by replacing it with an empty string. To use this method first inform the piece of text you want to replace and then the piece of text to replace it.

        var url = "https://www.udemy.com";

        console.log( url.replace( "https://" , "" ) ); // It will show "www.udemy.com"

There are many other operations we can do with strings but we are not going to explore them now. If you want you can learn more here.

### Contents