4. El discurso argumentativo
4.1. El concepto de ‘sentido’ en lingüística del texto
While Node.js theoretically supports the const keyword extension that some modern JavaScript implementations have implemented, it’s still not widely used. For constants, the standard practice is still to just use uppercase letters and variable declarations:
> var SECONDS_PER_DAY = 86400; undefined > console.log(SECONDS_PER_DAY); 86400 undefined >
27 Types
Numbers
All numbers in JavaScript are 64-bit IEEE 754 double-precision floating-point numbers. For all positive and negative integers that can be expressed in 2 53 bits accurately, the number type in
JavaScript behaves much like integer data types in other languages: > 1024 * 1024 1048576 > 1048576 1048576 > 32437893250 + 3824598359235235 3824630797128485 > -38423538295 + 35892583295 -2530955000 >
The tricky part of using the number type, however, is that for many numeric values, it is an
approximation of the actual number. For example: > 0.1 + 0.2
0.30000000000000004 >
When performing floating-point mathematical operations, you cannot just manipulate arbi- trary real numbers and expect an exact value:
> 1 - 0.3 + 0.1 == 0.8 false
>
For these cases, you instead need to check if the value is in some sort of approximate range, the size of which is defined by the magnitude of the values you are comparing. (Search the website stackoverflow.com for articles and questions on comparing floating-point numbers for good ideas of strategies on this.)
For those situations in which you absolutely need to represent 64-bit integer values in JavaScript without any chance of approximation errors, you are either stuck using the string type and manipulating these numbers by hand, or you can use one of the available modules for manipulating big integer values.
JavaScript is a bit different from other languages in that dividing a number by zero returns the value Infinity or -Infinity instead of generating a runtime exception:
> 5 / 0 Infinity > -5 / 0 -Infinity >
Infinity and -Infinity are valid values that you can compare against in JavaScript: > var x = 10, y = 0; undefined > x / y == Infinity true >
You can use the functions parseInt and parseFloat to convert strings to numbers: > parseInt("32523523626263"); 32523523626263 > parseFloat("82959.248945895"); 82959.248945895 > parseInt("234.43634"); 234 > parseFloat("10"); 10 >
If you provide these functions with something that cannot be parsed, they return the special value NaN (not-a-number):
> parseInt("cat"); NaN
> parseFloat("Wankel-Rotary engine"); NaN
>
To test for NaN, you must use the isNaN function: > isNaN(parseInt("cat"));
true >
Finally, to test whether a given number is a valid finite number (that is, it is not Infinity, -Infinity , or NaN ), use the isFinite function:
> isFinite(10/5); true > isFinite(10/0); false > isFinite(parseFloat("banana")); false >
Booleans
The boolean type in JavaScript is both simple and simple to use. Values can either be true or false, and although you technically can convert values to boolean with the Boolean
29 Types
function, you almost never need it because the language converts everything to boolean when needed, according to the following rules:
1. false , 0 , empty strings ( "" ), NaN , null , and undefined all evaluate to false .
2. All other values evaluate to true .
Strings
Strings in JavaScript are sequences of Unicode characters (represented internally in a 16-bit
UCS-2 format) that can represent a vast majority of the characters in the world, including those used in most Asian languages. There is no separate char or character data type in the language; you just use a string of length 1 to represent these. For most of the network applications you’ll be writing with Node.js, you will interact with the outside world in UTF- 8, and Node will handle all the details of conversion for you. Except for when you are manipulating binary data, your experience with strings and character sets will largely be worry-free.
Strings can be wrapped in single or double quotation marks. They are functionally equivalent, and you are free to use whatever ones you want. To include a single quotation mark inside a single-quoted string, you can use \' , and similarly for double quotation marks inside double- quoted strings, you can use \" :
> 'Marc\'s hat is new.' 'Marc\'s hat is new.'
> "\"Hey, nice hat!\", she said." '"Hey, nice hat!", she said.' >
To get the length of a string in JavaScript, just use the length property: > var x = "cat"; undefined > x.length; 3 > "cat".length; 3 > x = null; null
Attempting to get the length of a null or undefined string throws an error in JavaScript: > x.length;
TypeError: Cannot read property 'length' of null at repl:1:2 at REPLServer.self.eval (repl.js:109:21) at rli.on.self.bufferedCmd (repl.js:258:20) at REPLServer.self.eval (repl.js:116:5) at Interface.<anonymous> (repl.js:248:12) at Interface.EventEmitter.emit (events.js:96:17)
at Interface._onLine (readline.js:200:10) at Interface._line (readline.js:518:8) at Interface._ttyWrite (readline.js:736:14) at ReadStream.onkeypress (readline.js:97:10) To add two strings together, you can use the + operator: > "cats" + " go " + "meow";
'cats go meow' >
If you start throwing other types into the mix, JavaScript converts them as best it can: > var distance = 25;
undefined
> "I ran " + distance + " kilometres today"; 'I ran 25 kilometres today'
>
Note that this can provide some interesting results if you start mixing expressions a bit too much:
> 5 + 3 + " is my favourite number"; '8 is my favourite number'
>
If you really want “53” to be your favorite number, you can just prefix it all with an empty string to force the conversion earlier:
> "" + 5 + 3 + " is my favourite number"; '53 is my favourite number'
>
Many people worry that the concatenation operator + has terrible performance when working with strings. The good news is that almost all modern browser implementations of JavaScript— including Chrome’s V8 that you use in Node.js—have optimized this scenario heavily, and performance is now quite good.