JavaScript - Basic Data Types
// JavaScript types:
1. Number
2. String
3. Boolean
4. Function
5. Object
6. Null
7. Undefined
console.log(5..toString()); // 5
console.log(5.toString()); // SyntaxError: identifier starts immediately after
numeric literal
The simple types of JavaScript are numbers, strings, boolean (true or false),
null, and undefined. All other values are objects. Numbers, strings, and
booleans are object-like in the they have methods, but they are immutable.
JavaScript has a single number type. Internally, it is represented as 64-bit
floating point, the same as Java's double. Unlike most other languages, there
is no separate integer type, so 1 and 1.0 are the same value. This is a
significant convenience because problems of overflow in short integers are
completely avoided, and all you need to know about a number is that it is a
number. A large class of numeric type errors is avoided.
If a number literal has an exponent part, then the value of the literal is
computed by multiplying the part before the e by 10 raised to the power of the
part after the e. So, 100 and 1e2 are the same number.
Negative numbers can be formed by using the - prefix operator.
The value NaN is a number value that is the result of an operation that cannot
produce a normal result. NaN is not equal to any value, including itself. You
can detect NaN with the isNaN(number) function.
The value Infinity represent all values greater than 1.79….e+308
The value Infinity represents all values greater than 1.79769e+308.
Numbers have methods. JavaScript also have the Math object that contains a set
of methods that act on numbers. For example, Math.floor(number) method can be
used to convert a number into an integer.
A string literal can be wrapped in single quotes or double quotes. It can
contain zero or more characters. The \ (backslash) is the escape character.
JavaScript was built at a time when Unicode was a 16-bit character set, so all
characters in JavaScript are 16 bits wide.
JavaScript does not have a character type. To represent a character, make a string
with just one character in it.
The escape sequences allow for inserting characters into strings that are not
normally permitted, such as backslashes, quotes, and control characters. The \u
convention allows for specifying character code points numerically.
"A" === "\u0041"
Strings have a "length" property. For example, "seven".length is 5.
Strings are immutable. Once it is made, a string can never be changed. But it is
easy to make a new string by concatenating other strings together with the +
operator.
Two strings containing exactly the same characters in the same order are considered
to be the same string.
console.log('c' + 'a' + 't' === 'cat'); // true
Strings have methods:
console.log('cat'.toUpperCase()); // CAT
The simple types in JavaScript are numbers, strings, boolean (true and false), null,
and undefined. All other values are objects. Numbers, strings, and booleans are
object-like in that they have methods, but they are immutable. Objects in JavaScript
are mutable keyed collections. In JavaScript, arrays are objects, functions are objects,
regular expressions are objects, and of course, objects are objects.
An object is a container of properties, where a property has a name and a value. A
property name can be any string, including the empty string. A property value can be
any JavaScript value except for undefined. Objects in JavaScript are class-free. There
is no constraint on the names of new properties or on the values of the properties.
Objects are useful for collecting and organizing data. Objects can contain other objects,
so they can easily represent a tree or graph structure.
// Convert the string of any base to integer using JavaScript:
parseInt("4F", 16);
parseInt("123", 10);
parseInt("10011100", 2);
page revision: 9, last edited: 17 Nov 2016 23:41