Listado de la etiqueta: Types


PostgreSQL is an open-source object-oriented based database system. It is a powerful database system that supports both relational and non-relational data types. The Boolean data type is a commonly used data type that can accept three types of values: True, False, and NULL. The short form of this data type is bool and one byte is used to store the Boolean data. The True value of the Boolean data can also be denoted by ‘yes’, ‘y’, ‘true’, and 1. The False value of the Boolean data can also be denoted by ‘no’, ‘n’, ‘false’, and 0.

Pre-requisites:

You have to install the latest version of PostgreSQL packages on the Linux operating system before executing the SQL statements shown in this tutorial. Run the following commands to install and start the PostgreSQL:

$ sudo apt-get -y install postgresql postgresql-contrib

$ sudo systemctl start postgresql.service

Run the following command to login to PostgreSQL with root permission:

Use of Boolean data type in PostgreSQL tables:

Before creating any table with the Boolean data type, you have to create a PostgreSQL database. So, run the following command to create a database named ‘testdb’:

# CREATE DATABASE testdb;

The following output will appear after creating the database:

Example-1: Create a table using the Boolean data type

Create a table named ‘technicians’ in the current database with three fields. The first field name is tech_id, the data type is an integer and it is the primary key of the table. The second field name is name and the data type is character. The third field name is available and the data type is Boolean.

# CREATE TABLE technicians (

    tech_id INT NOT NULL PRIMARY KEY,
    name CHARACTER(10) NOT NULL,
    available BOOLEAN NOT NULL

);

The following output will appear if the table is created successfully:

Run the following command to INSERT a record into the technicians table where ‘true’ is used for the Boolean value:

# INSERT INTO technicians VALUES (1, ‘Esquivar Ali’, ‘true’);

The following output will appear after executing the above insert query:

Run the following INSERT command to insert a record into the technicians table where 0 is used for the Boolean value. The 0 is not acceptable for the Boolean value in PostgreSQL. So, an error message will appear.

# INSERT INTO technicians VALUES (2, ‘Kabir Hossain’, 0);

The following output will appear after executing the above insert query. The output shows an error message that indicates that the type of the 0 is Integer, not Boolean.

Run the following INSERT command to insert a record into the technicians table where ‘false’ is used for the Boolean value.

# INSERT INTO technicians VALUES (3, ‘Abir Hasan’, ‘false’);

The following output will appear after executing the above insert query:

Run the following INSERT command to insert a record into the technicians table where ‘t’ is used for the Boolean value:

# INSERT INTO technicians VALUES (5, ‘Rebeka Ali’, ‘t’);

The following output will appear after executing the above insert query:

Example-2: Create a table using Bool data type

Create a table named ‘products’ in the current database with three fields. The first field name is id, the data type is an integer and it is the primary key of the table. The second field name is name and the data type is character. The third field name is physical_product, the data type is BOOL, and the default value of the field is ‘true’.

# CREATE TABLE products (

    id INT NOT NULL PRIMARY KEY,
    name CHARACTER(10) NOT NULL,
    physical_product BOOL NOT NULL DEFAULT ‘true’

);

The following output will appear if the table is created successfully:

Run the following command to insert a record into the products table where ‘f’ is used for the BOOL value:

# INSERT INTO products VALUES (1, ‘Antivirus’, ‘f’)

Run the following INSERT command to insert a record into the products table where no value is provided for the BOOL data. Here, the default value of the field will be inserted.

# INSERT INTO products VALUES (2, ‘Celador’);

The following output will appear after executing the above two insert queries:

Check the content of the tables:

Run the SELECT following select query to retrieve all records from the technicians table:

# SELECT * FROM technicians;

Run the SELECT following select query to retrieve all records from the technicians table where the value of the available field is ‘false’:

# SELECT * FROM technicians WHERE available = ‘false’;

Run the SELECT following select query to retrieve all records from the technicians table where the value of the available field is ‘t’ or ‘true’:

# SELECT * FROM technicians WHERE available = ‘t’ OR available = ‘true’;

The following output will appear after executing the above three ‘select’ queries. The output of the first query shows all records of the table. The output of the second query shows those records of the table where the value of the available field is ‘f’. The output of the third query shows those records of the table where the value of the available field is ‘t’.

Run the following select query to retrieve all records from the products table:

# SELECT * FROM products;

Run the following select query to retrieve all records from the products table where the value of the physical_product field is ‘True’:

# SELECT * FROM products WHERE physical_product = ‘True’;

The following output will appear after executing the above two ‘select’ queries. The output of the first query shows all records of the table. The output of the second query shows those records of the table where the value of the available field is ‘t’.

Conclusion:

Different uses of Boolean or BOOL data types in PostgreSQL tables have been shown in this tutorial by using multiple examples to clarify the purpose of using Boolean data types in the table of the PostgreSQL database.



Source link


Date/Time data types are used in PostgreSQL to store date and time data. The Julian date is used in PostgreSQL for different types of date and time calculations. The most commonly used January to December calendar use Julian date. Five different data types are used in PostgreSQL to store date and time data. These are date, time, timestamp, time with time zone, and interval. The size and purpose of these data types have been shown below.

date data type:

It is used to store calendar date values in the date field of the database table. It takes 4 bytes to store the data. Different date input formats and date output formats are supported by the PostgreSQL database table which is explained below.

Date Input Formats:

Some commonly used date input formats are given below:

Format Description
Wednesday May 18, 2022 It uses the full weekday name, the full name of the month, the day of the month, and the year.
May 18, 2022 It uses the full name of the month, the day of the month, and the year.
2022-05-18 It uses standard ISO-8601 format that contains the numeric year, month, and day.
5/18/2022 It uses Non-European (U.S.) format that contains numeric month, day, and year.
18/5/2022 It uses a European format that contains numeric days, month, and year

Date Output Format:

The date output formats that are used in PostgreSQL are given below:

Format Description
PostgreSQL It uses the flamante PostgreSQL format.
Example: Wed 18 May 11:30:00 2022 PST
SQL It uses the traditional SQL format.
Example: 05/18/2022 11:30:00.00 PST
ISO It uses ISO-8601 standard format.
Example: 2022-05-18 11:30:00-07
German It uses the Regional Germany format.
Example: 18.05.2022 09:30:15.00 PST

time data type:

Time and time with time zone values take 4 bytes to store the data in the table of the PostgreSQL. Some valid time formats are given below:

Format Description
06:30 It uses ISO-8601 standard format.
07:20 AM It is similar to 07:20.
07:20 PM It is similar to 19:20.
15:15 It is similar to 03:15 PM.

Some valid time zone formats are given below:

Format Description
03:45:20-5 It uses ISO-8601 standard format that prints 7 hours behind GMT.
03:45:20-05:30 It uses ISO-8601 standard format that prints 7 hours and 30 minutes behind GMT.
03:45:20-0530 It uses ISO-8601 standard format that prints 7 hours and 30 minutes behind GMT.

timestamp data type:

Timestamp with time zone value takes 8 bytes to store the data in the table of the PostgreSQL. Some valid timestamp formats are given below:

Format Description
2022-05-25 12:25-7 It uses ISO-8601 date format with the minutes and PST time zone.
25/05/2022 12:30:20.100 It uses the European date format with microseconds.
05/25/2022 21:30 It uses the US date format with the minutes in 24-hour time.
25.05.2022 20:45:125 PM It uses the German regional date format with the seconds, and PM.

Pre-requisites:

You have to install the latest version of PostgreSQL packages on the Linux operating system before executing the SQL statements shown in this tutorial. Run the following commands to install and start the PostgreSQL:

$ sudo apt-get -y install postgresql postgresql-contrib
$ sudo systemctl start postgresql.service

Run the following command to login to PostgreSQL with root pemission:

PostgreSQL of date and time examples:

Run the following SELECT statement to read the particular date:

# SELECT date(‘2022-05-25’);

Run the following SELECT statement to read the current date and time:

Run the following SELECT statement to read the current date:

Run the following SELECT statement to read the current time:

The following output will appear after executing the above command:

Create a table using data and time data types:

Before creating any table with the Boolean data type, you have to create a PostgreSQL database. So, run the following command to create a database named ‘testdb’:

# CREATE DATABASE testdb;

The following output will appear after creating the database:

Create a table named ‘orders’ in the current database with five fields. The first field name is invoice_id, the data type is INT and it is the primary key of the table. The second field name is order_date and the data type is DATE. The third field name is order_time and the data type is TIME. The fourth field name is customer_id and the data type is VARCHAR(6). The fifth field name is entry_time and the data type is TIMESTAMP.

CREATE TABLE orders (
Invoice_id INT PRIMARY KEY,
order_date DATE,
order_time TIME,
customer_id VARCHAR (6),
entry_time TIMESTAMP);

The following output will appear if the table is created successfully:

Run the following insert query that will insert three records into the orders table. The order_date value has been given in ISO-8601 format. The order_time value has been given in ISO-8601 format. The current_timestamp value has been used in the entry_time field that will insert the timestamp value in ISO-8601 date format with the microseconds.

INSERT INTO orders
VALUES
(11785645, ‘2022-05-18’, ’10:30 AM’, ‘785634’, current_timestamp),
(11895634, ‘2022-05-17’, ’11:33 AM’, ‘856345’, current_timestamp),
(11128976, ‘2022-05-10’, ’08:30 PM’, ‘906745’, current_timestamp);

The following output will appear after executing the above query:

Run the following select query to read all records from the orders table:

The following output will appear after executing the above query:

Conclusions:

The date and time data types are required to store date and time data in the PostgreSQL tables properly. The PostgreSQL database supports different formats of date, time, and timestamp data types to store date and time data in the tables. The uses of these date and time data types have been shown in this tutorial by creating a table that will help the new PostgreSQL users to create the table by using these data types.



Source link


The character data types are used in the PostgreSQL database to store the different lengths of string data in the database tables. Three types of character data types are supported by PostgreSQL data types: char, varchar, and text. These data types are used for storing different types of string data. The purposes of using these data types have been explained below.

CHAR(n) or CHARACTER(n):

This data type is used to store the fixed length of string data. The length value will be provided at the time of table creation. The field of the table that contains char data will fill up the unused space with space characters. So, this data type is better to use for storing data of fixed length such as id of fixed length, phone number of a particular format, etc. The error will appear if the user tries to store string data larger than the length of the field of char data.

VARCHAR(n) or CHARACTER VARYING(n):

This data type is used to store the variable length of string data. The length will be provided at the time of table creation like the char data. The unused space of the field that contains this data type will remain unused and takes the space occupied by the string data only. The error will appear if the user tries to store string data larger than the length of the field of varchar data.

TEXT or VARCHAR:

This data type is used to store string data of unlimited length. So, the length value is not defined at the time of table creation. This is not SQL standard data type and it is removed from some relational database management systems (RDBMS) such as Oracle, MS SQL Server, etc.

Pre-requisites:

You have to install the latest version of PostgreSQL packages on the Linux operating system before executing the SQL statements shown in this tutorial. Run the following commands to install and start the PostgreSQL:

$ sudo apt-get -y install postgresql postgresql-contrib
$ sudo systemctl start postgresql.service

Run the following command to login to PostgreSQL with root permission:

Use of character data types in PostgreSQL:

Before creating any table with the Boolean data type you have to create a PostgreSQL database. So, run the following command to create a database named ‘testdb’:

# CREATE DATABASE testdb;

The following output will appear after creating the database:

Create a table:

Run the following SQL statement to create a table named clients in a PostgreSQL database by using different types of character data types. Here, the first field name is id which is the primary key of the table. The value of this field will be incremented automatically when a new record will insert. The name of the second field is name and the data type of this field is varchar which will be able to store a maximum of 15 characters. The name of the third field is address and the data type of this field is text. The name of the fourth field is email and the data type of this field is character varying that will be able to store maximum of 30 characters. The fifth field is phone and the data type is char which will store the string data of 14 characters.

# CREATE TABLE clients (
    id serial PRIMARY KEY,
    name varchar (15) NOT NULL,
    address text NOT NULL,
    email character varying (30),
    phone char (14) NOT NULL );

The following output will appear if the table is created successfully:

Insert data into the table:

Run the following insert query with the valid data to insert a record into the clients table:

# INSERT INTO clients (name, address, email, phone)
   VALUES (‘AR Company’, ’12/A, Dhanmondi, Dhaka.’, [email protected], ‘+8801826783423’);

The following output will appear if the insert query is executed successfully:

Run the following insert query to insert a record into the clients table. Here the value of the name field exceeds the size of the field. So, an error message will appear.

# INSERT INTO clients (name, address, email, phone)
   VALUES (‘RFL Company Ltd.’, ’23, Motijeel, Dhaka.’, [email protected], ‘+8801927891278’);

The following output will appear after executing the above insert query. Here, the name field can store a maximum of 15 characters but 16 characters are given in the query.

Run the following insert query to insert a record into the clients table. Here the value of the phone field exceeds the size of the field. So, an error message will appear.

# INSERT INTO clients (name, address, email, phone)
   VALUES (‘Vision’, ‘190/1, Mirpur, Dhaka.’, [email protected], ‘+880152678342312’);

The following output will appear after executing the above insert query. Here, the phone field can store 14 characters but 16 characters are given in the query.

Read data from the table:

Only one insert query has been executed successfully after executing the above three insert queries. Now, run the following select query to retrieve all records from the clients table:

The following output will appear after executing the above query:

Conclusion:

The character data type is a very useful data type of the PostgreSQL table. Some fields of the table require to store fixed-length of string data and some fields of the table require the variable length of data. The uses of three different data types of PostgreSQL tables have been explained in this tutorial by creating a table. The new PostgreSQL users will get proper knowledge about the character data types and will be able to use them properly for creating tables after reading this tutorial.



Source link


The sequential series of data is called range data. PostgreSQL database supports a range of data types which is a unique feature of this database. The range and multi-range data types are introduced in the PostgreSQL version 14. This data type stores the lower and the upper values. The integer or the timestamp value can be stored in the field of this data type. When it is required to store many values in a single range value then it is better to use range data type. The built-in range of data types that are supported by PostgreSQL has been mentioned below.

Range Types Purpose
int4range It defines the range of integers.
int4multirange It defines the multi-range integers.
int8range It defines the range of big integers.
int8multirange It defines the multi-range big integers.
numrange It defines the range of numbers.
nummultirange It defines the multi-range of numbers.
tsrange It defines the range of timestamp without the time zone.
tsmultirange It defines the multi-range of timestamp without the time zone.
tstzrange It defines the range of timestamp with the time zone.
tstzmultirange It defines the multi-range of timestamp with the time zone.
daterange It defines the range of date.
datemultirange It defines the multi-range of date.

Pre-requisites:

You have to install the latest version of PostgreSQL packages on the Linux operating system before executing the SQL statements shown in this tutorial. Run the following commands to install and start the PostgreSQL:

$ sudo apt-get -y install postgresql postgresql-contrib
$ sudo systemctl start postgresql.service

Run the following command to login to PostgreSQL with root permission:

PostgreSQL range data type examples:

Before creating any table with the Boolean data type you have to create a PostgreSQL database. So, run the following command to create a database named ‘testdb’:

# CREATE DATABASE testdb;

The following output will appear after creating the database:

A. Use of integer range data type
Create a table named ‘tbl_intRange’ in the current database with two fields. Here, the first field name is id which is the primary key of the table. The value of this field will be incremented automatically when a new record will insert. The second field name is int_range and the data type is INT4RANGE.

# CREATE TABLE tbl_intRange (
    id SERIAL PRIMARY KEY,
    int_range INT4RANGE );

The following output will appear if the table is created successfully.

Run the following INSERT query to insert three records into the tbl_intRange table:

# INSERT INTO tbl_intRange (int_range)
    VALUES (‘[1, 10)’::int4range),
              (‘[1, 20)’::int4range),
              (‘[1, 30)’::int4range);

The following output will appear after executing the above query:

Run the following SELECT query to read all records from the tbl_intRange:

# SELECT * FROM tbl_intRange;

Run the following SELECT query to read those records from the tbl_intRange WHERE int_range is greater than 12:

# SELECT * FROM tbl_intRange WHERE int_range @>12;

The following output will appear after executing the above SELECT queries:

B. Use of numeric range data type
Create a table named ‘tbl_numRange’ in the current database with two fields. Here, the first field name is id which is the primary key of the table. The value of this field will be incremented automatically when a new record will insert. The second field name is num_range and the data type is NUMRANGE.

# CREATE TABLE tbl_numRange (
    id SERIAL PRIMARY KEY,
    num_range NUMRANGE );

Run the following INSERT query to insert three records into the tbl_numRange table:

# INSERT INTO tbl_numRange (num_range)
    VALUES (numrange(20, 40)),
                  (numrange(100, 500));

The following output will appear after executing the above queries:

Run the following SELECT query that will read all records from the tbl_numRange:

# SELECT * FROM tbl_numRange;

The following output will appear after executing the above SELECT query:

C. Use of timestamp range data type
Create a table named ‘tbl_timeRange’ in the current database with two fields. Here, the first field name is id which is the primary key of the table. The value of this field will be incremented automatically when a new record will insert. The second field name is timeRange and the data type is TSRANGE.

# CREATE TABLE tbl_timeRange (
    id SERIAL PRIMARY KEY,
    timeRange TSRANGE);

Run the following INSERT query to insert three records into the tbl_timeRange table:

# INSERT INTO tbl_timeRange ( timeRange)
    VALUES (‘[2022-05-20 10:20:30, 2022-05-21 10:20:15)’),
                 (‘[2022-09-13 9:30:15, 2022-09-14 11:10:20)’);

The following output will appear after executing the above queries:

Run the following SELECT query that will read all records from the tbl_timeRange:

# SELECT * FROM tbl_timeRange;

The following output will appear after executing the above SELECT query:

D. Use of date range data type
Create a table named ‘tbl_dateRange’ in the current database with two fields. Here, the first field name is id which is the primary key of the table. The value of this field will be incremented automatically when a new record will insert. The second field name is date_range and the data type is DATERANGE.

# CREATE TABLE tbl_dateRange (
    id SERIAL PRIMARY KEY,
    date_range DATERANGE);

Run the following INSERT query to insert three records into the tbl_dateRange table:

# INSERT INTO tbl_dateRange ( date_range)
VALUES (‘[2022-05-20, 2022-05-21)’),
                  (‘[2022-07-10, 2022-07-15)’),
                  (‘[2022-12-01, 2022-12-16)’);

The following output will appear after executing the above queries:

Run the following SELECT query that will read all records from the tbl_dateRange:

# SELECT * FROM tbl_dateRange;

Run the following SELECT query that will read those records from the tbl_dateRange where the date value of the date_range field is greater than ‘2002-07-13’.

# SELECT *FROM tbl_dateRange WHERE date_range @>‘2022-07-13’::date;

The following output will appear after executing the above queries:

Conclusion:

Different uses of range data types of PostgreSQL have been shown in this tutorial by using multiple tables. The new PostgreSQL users will be able to use the range of data types in their tables after reading this tutorial.



Source link


In JavaScript, literals are a way to represent values in a program. Let’s take a look at a scenario where you need to use some code that provides the same functionality everywhere in a program like a template.

Here JavaScript literals come into the picture which provides the user with predefined functionalities when they use it.

This article is a complete guide and occupied with the detailed knowledge about

  • What are JavaScript literals
  • Types of JavaScript literals
  • Template Literals
  • Object Literals
  • String Literals
  • Numeric Literals
  • Boolean Literals
  • Floating-Point Literals
  • Regular expression Literals

What are JavaScript Literals

JavaScript Literals are the fixed values that are used to represent data in a program. These literals are used to represent data like integer, string, boolean, and array. We do not need any specific keyword to write these literals.

Types Of JavaScript Literals

Following are the types of literals that are supported by JavaScript:

  • Array Literals
  • Boolean Literals
  • Floating-point Literals
  • Numeric Literals
  • Object Literals
  • Regular Expression Literals
  • String Literals
  • Template Literals

Array Literals

A collection of elements wrapped between the pair of square brackets [ ] represent an array idéntico in JavaScript. These literals are initialized by the specific values added between square brackets. The size of the array idéntico is specified by the number of elements between square brackets.  Array idéntico may contain zero or more elements according to the programmer’s requirement.

Code

// Array Fiel with 0 element
var fruit3 = [‘Mango’,‘Watermelon’,‘Pineapple’];
console.log(fruit1);

// Array Fiel with elements
var fruit3 = [‘Mango’,‘Watermelon’,‘Pineapple’];
console.log(fruit2);

// Array Fiel with extra comma between elements
var fruit3 = [‘Mango’,,‘Watermelon’,‘Pineapple’];
console.log(fruit3);

Here we create three array literals.

Output

In the above example, we create three arrays fruit1, fruit2, and fruit3 using an array idéntico.

We add no elements in the first array which is considered as an array in JavaScript but with no element.

In the second array, we add three elements that initialize the array as string type due to the elements in it, and its size is specified as 3 because of the number of elements in it as shown in the above output.

In the third array, we also add three elements but put an extra comma between the elements due to which the compiler considers the length of the array to be 4 but with one empty index as shown in the above output.

Note: If we put a comma at the start of the elements or between the elements, the compiler considers it as an index but if we put a comma at the end of the elements, the compiler ignores it completely.

Boolean Literals

In JavaScript boolean idéntico works with comparison operators like <, >, <=, >=, ==, != etc. The functionality of these literals is predefined as these literals only return true or false.

Code

var check = (20>43);
console.log(`20 > 43 = ${check}`);

var comp = (7>3);
console.log(` 7 > 3 = ${comp}`);

Here we take two variables, check and comp and compare different values in both variables which will return only true or false as an output because both variables are using boolean idéntico.

Output

As in the above output it is clearly shown that the check variable returns a false value and the comp variable returns a true value as they both used boolean literals.

Floating-point Literals

These literals represent the values with a quebrado point. Floating-point literals can be a quebrado point number, a quebrado point integer, or an exponent.

Code

var dec = 3.5;
console.log(`This variable represent quebrado number ${dec}`);

var fra = 30.6;
console.log(`This variable represent fractional number ${fra}`);

var exp = 12e6;
console.log(`This variable represent exponent of number ${exp}`);

Here we take three variables dec, fra and exp. Then assign a positive quebrado point value to dec, negative quebrado point value to fra, and exponent value to exp.

Output

The above output clearly shows that the dec variable prints a positive quebrado point value. The quebrado point value is always positive.

The fra variable print quebrado point integer value which means floating-point idéntico can be a positive or negative number with a quebrado point.

The exp variable print exponent of a number which means floating-point idéntico can be used to represent an exponent of a number.

Numeric Literals

Numeric literals are basically the series of digits/numbers. Numeric literals can be represented in:

Colchoneta 10: quebrado (which contains digits from 0 to 9)

Colchoneta 8: octal (which contains digits from 0 to 7)

Colchoneta 16:  hexadecimal (which contains digits from 0 to 9 and the letters from A/a to F/f)

Code

var dec = 35;
console.log(`This variable is a quebrado number ${dec}`);

var oct = 062;
console.log(`This variable is an octal number ${oct}`);

var hex = 0X8b;
console.log(`This variable is a hexadecimal number ${hex}`);

Here we take three variables dec, oct and hex, then we assign a quebrado value to dec, octal to oct, and hexadecimal value hex.

Output

In the above output it is clearly seen that the dec variable prints the quebrado number. The oct variable takes an octal number and prints the value after converting it into a quebrado number. The hex variable takes a hexadecimal number and prints the value after converting it into a quebrado number.

Object Literals

Object idéntico is basically a list of 0 or more pairs of property names and associated values of an object wrapped inside a pair of { } curly brackets.

Code

var info = {name:«Alex», roll no:«35», marks:«85»};
console.log (`${info.name} got ${info.marks} marks.`);

Here we create a variable info and assign a list with name, roll number and marks to it. Then we access names and marks from the list with help of a (.) and print the result.

Output

As above, the output shows that we get the expected output with the help of object idéntico.

Regular Expression Literals

Regular expression literals are mainly used to quickly search long information in long strings. These literals are used with forward slashes (//). The word that is to be searched in a string wrote between forward slashes as shown below

Code

var str =«This is alex from abc company»
var str2= /from/;
var search = str.match(str2);
console.log(search);

Here we take three variables str, str2, and search. Then we assign a string to str, str2 is assigned with regular expression idéntico, and search is assigned with a JavaScript match() function and gives str2 as a parameter. Lastly, we display the result.

Output

Above output clearly shows that we search (from) word from the given string with the help of regular expression idéntico and it displays the word that is searched, the index number where that word is placed, and the string from which that word is searched.

String Literals

A string idéntico is basically a string made up of characters between (“”) double or (‘’) single quotation marks.

Following are the special characters used in JavaScript string literals.

Characters Explanation
n Add a new line in a string.
f Add form feed in a string.
b Add backspace in a string.
t Add a tab in a string.
r Used for carriage return in a string
Add backslash () inside a string.
Add double quote (“) in a string.
Add double quote (‘) in a string.

Code

var str =«This is alex n from abc company.»
console.log(str);

var str =‘This is alex t from abc company.’
console.log(str);

Here we take two strings. The first one is represented with double quotes string idéntico and n special character, the second one is represented with single quotes string idéntico and t special character.

Output 

Above output clearly shows that both string literals, double and single quote print strings but n starts a new line while t adds a tab in a string.

Template Literals

String and variables combined together between the pair of backticks (“) are defined as template literals. These literals are used to combine strings and variables without making the code messy. String interpolation is also performed using template literals.

Code

a = 5;
var str =`${a} is an odd number.`
console.log(str);

Here we take a variable a and str, then we assign 5 to variable a and use template idéntico on variable str. Then we simply display the str variable.

Output

Above output clearly shows that we get the required output using template literals.

Conclusion

JavaScript literals are the fixed values that are assigned to variables to represent different data. This article explains the types of JavaScript literals, like an array, string, floating-point, object, template, and numeric literals in detail.



Source link