Primitive data types

Now you know what variables are and why we need them in our code, it is time to look at the different types of values we can store in variables. Variables get a value assigned. And these values can be of different types, JavaScript is a loosely typed Language. This means that JavaScript determines the type based on the value. The type does not need to be named explicitly. For example, if you declared a value of 5. JavaScript will automatically define it as a number type.

A distinction exists between primitive data types and other, more complex data types. In this chapter, we will cover the primitive type, which is a relatively simple data structure. Let’s say for now that they just contain a value and have a type Javascript has seven primitives: String, Number, Bigint, Boolean, Symbol, undefined. and null. We’ll discuss each of them in more detail below.

String

A string is used to store a text value. It is a sequence of characters. There are different ways to declare a string.

  • Double quotes
  • Single quotes
  • Backticks:special template strings in which you can use variable directly the single and double quotes can both be used like so:
let singlestring = 'Hi there!'; let doublestring = "How are you?";
NOTE

You can use the option you prefer, unless you are working on code in which one of these options has already been chosen. Again, consistency is key.

The main difference between single quotes and double quotes is that you can use single quotes as literal characters in double-quoted strings, and vice versa. If you declare a string with single quotes, the string will end as soon as a second quote is detected, even if it’s in the middle of a word. So for example, the following will result in an error, because the string will be ended at the second single quote within let’s:

let funActivity = let's learn javascript';

Let will be recognized as a string, but after that, the bunch of characters that follow cannot be interpreted by JavaScript. However, if you declare the string using double quotes, it will not end the string as soon as it hits the single quote, because it is looking for another double quote. Therefore, this alternative will work fine:

let funActivity = "let's learn javascript";

In the same way with double quotes, the following would not work:

 

let question = "Do you want to learn javascript? "Yes!"";

Again, the compiler will not distinguish between double quotes used in different contexts, and will output an error.

In a string using backticks, you can point to variables and the variable’s value will be substituted into the line. You can see this in the following code snippet:

let language = "javascript";
let  message = 'let's learn ${language}';
console.log(message);

As you can see, you will have to specify these variables with a rather funky syntax-don’t be intimidated! Variables in these template strings are specified between $(nameOfVariable) The reason that it’s such an intense syntax is that they want to avoid it being something you would normally use, which would make it unnecessarily difficult to do so. In our case, the console output would be as follows.

As you can see, the language variable gets replaced with its value : javascript.

let's learn javascript

Escape characters

Say we want to have double quotes, single quotes, and backticks in our string. We would have a problem, as this cannot be done with just the ingredients we have now There is an elegant solution to this problem. There is a special character that can be used to tell JavaScript, “do not take the next character as you normally would.” This is the escape character, a backslash.

In this example, the backslash can be used to ensure your interpreter doesn’t see the single or double quote marks and end either string too early.

let str = "Hello, what's your name? is it \"mike\"?";
console.log(str);
let str2 = 'Hello, what\'s your name? is it "mike"?';
console.log (str2);

This logs the following to the console:

Hello, what's your name? is it "mike"?
Hello, what's your name? is it "mike"?

Discover the Different Primitive Data Types in JavaScript

By explaining each data type and providing examples, visitors will leave the page with a deeper understanding of how to use and manipulate data in their JavaScript code.

Number of Statistics
0
Total Number of Stats
0 +
Statistics Overview
0 +
Statistics Summary
0 +
Skip to content