So the continuation of the first part, a lot of time has passed, and now ... here I, as promised, will tell you in more detail about the main types. To begin with a couple of comments, the first part did not pass naturally without criticism, so the introduction will be 'work on mistakes'. In the future, such work on the errors will be inserted regularly, and I hope the habra people will help this ...
So let's repeat the types:
data types are of 2 types: simple and reference. The difference is that when we assign a variable of a simple type to another, it copies the value, and when we assign a variable of a reference type, the second variable refers to the value of the first ... for example:
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
// var intVal = 5; // () var otherIntVal = intVal; // , // 5 ( otherIntVal); // var arrayVal = [1,2,3,4]; // - 4- ; var otherArrayVal = arrayVal; // , // (4 ) , , otherArrayVal[1] = 55; // , alert( arrayVal[1] ); // 2 ( 0), 55 * This source code was highlighted with Source Code Highlighter .
and a few words about special meanings:
NaN is a value that means “not a number”. It appears during arithmetic operations, when the result is not numeric, or when converting a string not starting from a number into a number (for example, 'qwe123').
null - nothing (empty value).
undefined - this value indicates that the variable (property) does not exist, or the value has not yet been assigned (var someVariable;).
infinity - infinity, obtained by dividing by 0 and going beyond the maximum / minimum range of the type
Speaking of NaN ... it’s a unique value in general, and in the literal sense of the word ... it appears when you convert it incorrectly to a number, but it’s not equal to anything, even to yourself =), in order to check whether we have a number, use the isNaN function , (!!!) it returns false if it receives the value as a number (isNaN = is Not A Number).
isNaN (5); // false
isNaN ('string'); // true
check what type of variable can be using the typeof function, it will return the type as a string.
typeof (4) // 'number'
typeof ("some string") // 'string'
typeof ([]) // 'object' - yes, it will call an array an object (how to distinguish them further),
// in fact, the array is an object, I specifically brought it into a separate group, mine is worth it ...
// but this is not the most interesting
typeof (null) // 'object' - here he will also say that this is an object
And once again I will list the main data types:
boolean - a boolean value, it has 2 options, either true (true) or false (false)
number is a number, an integer, or with a fractional part (they differ if necessary).
string - a string, a certain sequence of characters.
array is an array of data (actually an object).
object is an object.
function is the function itself.
More than once asked the question about equality ... there are 2 options for comparison: == and === what is the difference?
in JS, there are no types as such, they are all just in words and taken on the basis of the current value, they are usually not needed, for example, if we have a string representation of a number, then when JS is divided, the string is automatically converted to a number, for example:
var q = '55';
var w = '5';
alert (q / w) // result 11
alert (5 == '5'); // true;
but when comparing, it is sometimes necessary to determine the exact type, for example, when we compare 0 and ''
alert (0 == ''); // true
in this comparison, JS sees that both values are empty, and equates them to avoid this, you need to tell JS that the types also need to be taken into account, === this is exactly what makes
alert (5 === '5') // false value is similar, but the number is not equal to the string
alert (0 === '') // false number is not equal to string
And now briefly about the main thing ...
deleting variables:
you can assign an empty value (null, undefined),
or delete it using the delete operator (delete someVar;) with the help of the delete operator you can delete the properties of objects
')
type boolean:
values take 2 forms - true / false (true / false)
type number:
can be created either with the help of the constructor, or simply by assigning the desired value
var intVar = 5; // creation by assigning a value, the type is set automatically
var intVar = new Number (5); // create a number using the constructor.
var intVar = new Number ('5'); // you can give the initial value as a string, it is automatically converted to a number
var intVar = new Number ('q'); // you can even do this, but the value will be NaN
In general, in these methods of creation there is a slight difference, except that you can earn NaN through the constructor, typeof will produce 'object' when created through the constructor.
The number can be 2 types: integer and floating point. In principle, there is no special separation, they are automatically converted into each other during arithmetic operations, but if you need to separate them for sure, there are 2 functions: parseInt and parseFloat, they convert the variable to integer and floating point, respectively. (The integer does not make sense to convert to a floating point number, but the lines are converted with a bang).
The number has a couple of useful methods (yes, the numbers also have methods)
.toFixed () - translates a number into a float if it is not, and cuts it to the required number of decimal places
toExponential () - converts a number into an exponential form (5.000 + 0)
toPrecision () - automatically leads to an exponential form, if the number is large
Floating point problem
alert (0.1 + 0.7) // 0.7999999999999999
This must be taken into account, this is not the only example of inaccuracy, there are many such options, the problem is solved by rounding, here equality is powerless:
alert ((0.1 + 0.7) .toFixed (9)) // "0.800000000"
string type:
You can also, like number, create by simply assigning a value, and through the constructor
var strVal = 'some string'; // create by assigning a value
var strVal = new String ('some string') // creation through constructor
var strVal = new String (5) // creation through constructor
as in the case of a number, if you create a variable through the constructor, typeof will produce 'object', be careful.
strings "understand" special characters, they are not displayed when outputting, but they can severely damage it, even errors ... these are the main
\ n - Newline character
\ r - carriage return
\ t - Tab
\ v - Vertical tab (IE does not understand this character, therefore it simply ignores '\' and thinks that it is the letter 'v')
\ '- single quote
\ "- Double quote
\\ - Backslash (\)
example:
var strVal = 'some string' and some explain 'some other string' // this cannot be done
var strVal = 'some string \' and some explain \ 'some other string' // need to escape quotes like this
alert (strVal); // the slash is no longer visible (they did their job), instead of them beautiful quotes
useful properties of strings:
.length // it is one, but nowhere without it, returns the length of the string
// I advise you to pay attention: the first character has a position 0, and the length is counted from 1
// finally we have the position of the last character = length-1
Some useful string methods:
indexOf () - returns the index of the first match of the search string.
replace () - replaces the text with another, can be used with a regular expression
slice () - returns a piece of text starting from the given, to the specified position
substr () - returns a piece of text starting at the specified position, specified length
toLowerCase () - 'omits' the string in small letters
toUpperCase () - raises the string in large letters
type array (in fact, it is an object, but it has its own methods and properties):
You can create an array in two ways:
var someArray = new Array (); // via constructor
var someArray = []; // via object literal, this method, though less visual, is more convenient.
in JavaScript, the array is “rubber”, it is not necessary to reserve the number of elements for it, it increases automatically, when you delete an element in the middle, the array is not compressed, instead, the deleted element will be undefined
dynamically add values to the end (like in php: array [] = '2', you must specify the index, or use the appropriate methods)
Some useful properties:
length - returns the number of elements (if you assign it a value less than the length of the array, the array will be cut to the assigned value, if you assign 0, the array will be cleared)
Some useful methods are:
join () - connects the elements of an array into a string, you can specify a separator
pop () - removes and returns the last element of the array
push () - writes an element to the end of the array
reverse () - reverses the order of elements
splice () - removes the specified number of elements starting at the specified position
sort () - sorts the elements of an array
object type:
The object can be created in 2 ways:
var obj = new Object (); // using the constructor
var obj = {}; // using object literal
var obj = {'key': 'value', 'key2': 'value2'} // declaration with initial values
if you refer to the non-existent properties of the object, then there will be no errors, the value will be undefined
Here, the main things that you regularly come across when you access non-existent variables, an error occurs (yes, this may be, some create variables in if), you can avoid this (and other errors) with this design:
- try
- {
- // here we write the problem code
- }
- catch (e)
- {
- / * this code will be executed only if an error occurs in the problem code
- e is an error object, it has the following properties:
- fileName - the file in which the error occurred
- lineNumber - the line in which the error occurred
- message - error description message
- may be, depends on the browser * /
- alert (e.message);
- }
- finally
- {
- // this code will be executed in any case, there will be an error or not
- }
* This source code was highlighted with Source Code Highlighter .
In principle, such a construction exists in almost all languages, and it works in the same way.
The if / while / for constructs are similar to c-like syntax languages, the only difference is the enumeration of object properties / methods
for (key in obj)
{
// key - the key in text form
someVar = Obj [key]; // get the property / method of the object
}
// essentially a JS object is a hash, that is, a banal key-value mapping (Alya associative array) and the value can be any, starting with simple values (number, string) and ending with functions (these will be methods).
So far, everything, further functions are planned, functions as a constructor (such as classes), closures (in passing, just a couple of examples to understand what it is and why you need it), let's take a closer look at the objects and lastly a couple of words about prototypes - expanding our own and standard 'classes' (of type String and Number)