Top image

WEB design and programming - HTML | CSS | Java Script | PHP

Basic JavaScript

JavaScript: Data types & Literal

What is a Data types?

WHAT IS A DATA TYPES?

Data Types is a basic type of JavaScript.

JavaScript has a Data Types which consist of 6 kinds of primitive types and objects.
When we categorize the data types, it could be two types called Primitive type and Object (Composite types).

Here’s a schematized Data types:

Scheme of Data types

Primitive types

Number

any number without quotes:
0, 2, 3, 4, 5, 6, 7, 8, 9, 0.5

String

characters wrapped in single or double quotes:
“apple”, “JavaScript”, “WEB DESIGN”, “I’am a genius”

Boolean

true or false, means “Yes(that is correct)” or “NO(that is wrong)”.

Null

This data type represents a intentional absence of a value: null

Undefined

This data type represents that a value is undefine: undefined

[ES2015]Symbol

A newer feature in [ES2015]ES6 which is latest type of JavaScript. They don’t have specialized function or unique identifier.

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.

JavaScript has an expression of Literal defined as a syntax which will be able to write a value of data types directly.

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:

> Booleans
> Numbers
> Null
> Strings

And at part of Object type also has a special literal:

> Object
> Array

>>About an object literal is here.

Literal of Primitive types

Boolean literal

They have both literals, true or false. They will return true or not, just as it is to see.

true; // => true
false; // => false

Number literal

console.log(1); // => 1
console.log(20); // => 20
console.log(333); // => 333

Null literal

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.

NOTICE:

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.

String literal

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

NOTICE:

> 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

Without the literal representation, the computer will not treat the above-mentioned repeating strings, numbers, and Booleans as values. Therefore, you would have to define a new function to make the value understood as a value in that case. In order to avoid such trouble, JavaScript provides a literal expression that can directly handle data types as values.

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 a basic type of JavaScript.
> 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

JavaScript: Variable

▼ previous post as well

Table of contents you can learn JavaScript basis