WHAT IS A DATA TYPES?
When we categorize the data types, it could be two types called Primitive type and Object (Composite types).
Here’s a schematized Data types:
any number without quotes:
0, 2, 3, 4, 5, 6, 7, 8, 9, 0.5 …
characters wrapped in single or double quotes:
true or false, means “Yes(that is correct)” or “NO(that is wrong)”.
This data type represents a intentional absence of a value: null
This data type represents that a value is undefine: undefined
Object (Composite types)
Object type is a collections of related data which consists of several primitive types and objects: Objects, Functions, Arrays and so on. We will be doing to learn it in a chapter of Objects.
WHAT IS A LITERAL?
Literal is an expression defined as a syntax.
Here’s what literal look like:
const str = "Hello"; // One wrapped by " " is the literal.
In Primitive type, there are 4 kinds of an expression of literal:
And at part of Object type also has a special literal:
>>About an object literal is here.
Literal of Primitive types
They have both literals, true or false. They will return true or not, just as it is to see.
true; // => true false; // => false
console.log(1); // => 1 console.log(20); // => 20 console.log(333); // => 333
The literal for null will return null value that means there is no value intentionally.
const userEmail = null; console.log(userEmail); // => null
We would use null or undefined at times to define an absent value on purpose because by doing that, it will be helpful to store the values generated later (for example, newly registered user information) irregularly.
Though null is the literal, undefined is global variable. Meaning you can not define null as a variable name but if technically to say, undefined could be.
There are three types of literals to express the strings. However, results will be a such same evaluation : string.
console.log("string"); // => "string" //double quotes console.log('string'); // => "string" //single quotes console.log(`string`); // => "string" //backticks
> Double quotes and a single quotes are same meaning
> Use the other quote that not appear with in the string when you want to use certain quote mark with in the string like this: `I said to him, “We will be broken” `
> Or use a \ in front of what you want to use like: “I said to him, \”We will be broken\” “, which is called Escape.
[ES2015] Template Literal
A template literal is wrapped by backticks `. The template literal is readable, become a code shorter, and that you don’t need to concern to part whether you should use “”, ” or “.
In the template literals, you can start a new line without \n, which is a old written way to create the new line.
Here’s example of the template literal:
`My name is Achi`; // In the past, you wold need to express "My name\nis\nAchi"
>> More information about Template Literal
Necessity of literal
Literals are like reserved words that cannot be defined as variables, and attempting to redefine them will cause a Syntax Error. In other words, the data types with defined registry expressions such as true, false, and null cannot be redefined and there will be a syntax error if you try to use null, true or false, as variable names.
REVIEW OF DATA TYPES
> Data Types is consist of 6 kinds of primitive types and some composite types called an object.
> We can use an expression of literal when we use a Data Types.
> We can’t define literal as a variable again since it would emerge an error.
> ES6 provides us the template literals to clarify a code.
▼ Check out the new post
▼ previous post as well