What are literal types in Javascript?

Asked

Viewed 391 times

4

I’ve seen several times the terms in the programming of literal types, but I don’t know what that is, for example, literal string, literal numbers, literal objects and so on. What is that? There’s a difference for a normal guy?

3 answers

4

Literals are not types, this term is wrong. Literals have a type, it’s different. So it’s not like there’s no difference to a "normal guy," there’s no relationship, and there’s no normal guy, like it’s like. If you read the term that way from the question, run away from the material.

So the correct thing is to have a literal string, or literal number, or literal object. Probably the term was used this way by bad translation.

Values have types. A literal is a value. In Javascript variables do not have types, only values. A literal is just a way to express a value in the code.

What is a literal? has already been well answered, has not to answer again that part.

The literal then is a "texto aqui" or is a 12 or a { x : 1, y : 2 } to quote the three that are in the question. Just this, any other part of the code is not the literal.

4


Literals are exactly write the value, for example this would be a "literal array":

var x = [ "a", "b", "c" ];

This is no longer:

var x = new Array();

x.push("a");
x.push("b");
x.push("c");

"Literal" is a value that you write literally in your script, can be assigning to a variable, or passing with argument from a function, or even writing loose in your script, without assigning or setting anything, what matters is that you write exactly what the value will be (of course having certain situations like "escapes" and other details).

Boolean Literal

Is to write true and false directly in the script assigning its variable or setting somewhere:

var x = true;

Here x has assigned a non-literal value, (the value is still true):

var x = !!1;

If the return of a function or a directly assigned condition causes true or false will not be literal, for example:

 var x = condicao1 || condicao2;

This isn’t either:

 var x = foo();

 function foo() {
    return condicao1 || condicao2;
 }

Regex Literal

In regex most already know and end up using the same literal as the examples:

/foo/
/foo/g
/foo|bar/gi

But there is the "non-literal", which would be useful to dynamically customize your regex:

new Regex("foo")
new Regex("foo", "g")
new Regex("foo", "gi")

These values are equivalent to literals, but you could adjust via values and variables

Literal object

The literal object is written between {...}, a literal object may contain

Literal string

Literal strings can be written:

'foo'
"bar"
'foo \n bar'
"baz' bar"
"baz\" bar"

Note that line breaks used the \n, and has several "escapes" as \0, \t, etc, but writing them will not cease to be literal, because you are writing the value in a certain way, Javascript had (non-standard) means to break lines within script (not value) using the \, thus:

var x = 'foo\
bar\
baz';

It would be a means of breaking long lines, but from ES6 we have the Literal template (previously called "Template strings")

`foo bar`

`foo
bar`

var a = 1, b = 2;

`foo ${a + b} bar`

`foo ${a * b} bar`

Note that it was possible to break lines "in value" in the second example.

In the third and fourth example you can notice the expressions within ${...}, having expressions does not change the fact that the template is literal, because literal is the "template" in this case.


There are a few things about ES6 that I need to add, like "contain" arrays of all items, example [ "a", , "c"], in this case index 1 would return undefined, the array remains literal of course, with time I add these details is just an initial response.

  • I think I understood what literal values are, just to confirm my understanding on the subject, for example, literal objects, literal strings and etc are values that are written clearly as, for example, 'Olá', 25, {n1: 30} etc? and already nonliteral values are values that are written obscurely as, for example, 10 < 10, typeof n1, etc.?

  • @felipecardozo basically that same, so I quoted the examples, in literals you write exactly what it will be, anything else not ;)

  • Thanks friend, now it all makes sense, thank you :)

  • @felipecardozo no, that’s not it. At least not in one of the examples he gave.. In the code 10 < 10 for example it has 2 literals, equal by the way they form an expression, but the whole code is not a literal. Literal is a form of expression. So I put in my answer a link which correctly explains how a literal is, I did not put here because the question would be duplicated and closed. Literal is one of the simplest things there is to understand, read there.

  • Thanks, @Maniero, I was wrong in this example quoted.

2

Font and for more details: MDN-Javascript, Syntax and types

Javascript literals are fixed values, not variables, which literally are inserted into your script.
The literals in Javascript are:

  • Literal array
  • Literals
  • Floating point literals
  • Integer
  • Literal object
  • Literal string

Literal array

A literal array is a list of zero or more expressions, each of which represents an element of the array, inserted between brackets ([]):

var coffees = ["French Roast", "Colombian", "Kona"];

You do not need to specify all elements in a literal array. If you place two commas on a line, the array is created with Undefined for the unspecified elements.

var fish = ["Lion", , "Angel"];

Literals

The logical type has two literal values: true and false.

var x = false;

Integer

Integers can be expressed as decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2).

  • Literal integer decimal consists of a sequence of digits without a 0 to the left.
  • 0 left on a literal integer indicates that it is in format octal. Octal may include only the digits 0-7.
  • 0x(or 0X) indicates a hexadecimal. Hexadecimal integers can include digits (0-9) and letters a-f and A-F.
  • 0b (or 0B) indicates a binary. Binary integers may include only the digits 0 and 1.

Some examples of literal integers are:

0, 117, -345 (decimal, base 10)
015, 0001,  -077 (octal, base 8) 
0x1123, 0x00111,  -0xF1A7 (hexadecimal, "hex" or base 16)
0b11, 0b0011, -0b11 (binário, base 2)

Floating point literals

Have the following syntax:

[(+|-)][digits][. digits][(E|e)[(+|-)]digits]

For example:

3.1415926
-.123456789
-3.1E+12
.1e-23

Literal object A literal object is a list of zero or more pairs of property names and associated values of an object, placed between keys ({})

var carro = { carros: {a: "Saab", "b": "Jeep"}, 7: "Mazda" };

Browser other questions tagged

You are not signed in. Login or sign up in order to post.