
Javascript from A to ...
I began to notice that articles on Habré appeared on programming, maybe phrases such as “Habr is not the same” came across this, but this is not so important, because I like it, I always wanted to learn something new ... my first article led to a huge collapse , therefore, after reading people, I will repeat my efforts and begin the cycle of notes about JavaScript from the very beginning and until it is in demand =)
So part one: the basics.
To begin with, we will decide on my opinions: I am not a master in theory, so in order not to burden you with unnecessary names (I will indicate doubts), I will simplify a lot of things.
Data types:
boolean - a Boolean value, it has 2 options, either true (true) or false (false)
number - a number, integer, or with a fractional part.
string - a string, a certain sequence of characters.
array - an array of data.
object - an object.
function - the function itself.
Variables can be declared using the var keyword .
but you can declare without var
numberVariable = 4;
stringVariable = “text”;
so they become global and visible everywhere
there are also some special values:
NaN is a value that means “Not a Number”. It appears during arithmetic operations when the result is not numerical.
null - nothing (empty value).
undefined - this value indicates that the variable (property) does not exist, or the variable has no value (as opposed to null, where the variable has a value, but empty),
Infinity - infinity (which is obtained at 1/0) (thanks 1602 for the reminder)
NaNgetting is very simple: parseInt ("q") + 5; // parseInt - translates other types into a number
to check the variable for existence, you need to compare it with undefined
but there is one thing, but if JS has never heard anything about this variable, then there will be an error, the question immediately arises, what kind of nafik? And how to avoid this if you need to check a variable? You can avoid this as follows:
Finally, a couple of words about var : an example of how var can interfere with expectations:
many will expect the result: 000111222, but in fact the result will be 000 ... everything will change only var inside the function, which will separate the outer q from the inside q in the function,
this is the end of the first part, then I will talk a little about simple types (number, string, Boolean variables ), some ways of working with them, and about type casting
So part one: the basics.
To begin with, we will decide on my opinions: I am not a master in theory, so in order not to burden you with unnecessary names (I will indicate doubts), I will simplify a lot of things.
Data types:
boolean - a Boolean value, it has 2 options, either true (true) or false (false)
number - a number, integer, or with a fractional part.
string - a string, a certain sequence of characters.
array - an array of data.
object - an object.
function - the function itself.
Variables can be declared using the var keyword .
- var numberVariable = 4;
- var stringVariable = "text";
- var arrayVariable = new Array(1, 2);
- // есть и короткое обьявление массива
- var arrayVariable2 = [1, 2];
- var objectVariable = new someClass();
- // и опять же короткое обьявление
- var objectVariable2 = { 'key': 'value', key2: 'value2' }
- // кавычки у второго ключа я пропустил не по ошибке,
- // если не используете ключевые слова то на ключах их можно не ставить, хотя лучше это делать
* This source code was highlighted with Source Code Highlighter.
but you can declare without var
numberVariable = 4;
stringVariable = “text”;
so they become global and visible everywhere
there are also some special values:
NaN is a value that means “Not a Number”. It appears during arithmetic operations when the result is not numerical.
null - nothing (empty value).
undefined - this value indicates that the variable (property) does not exist, or the variable has no value (as opposed to null, where the variable has a value, but empty),
Infinity - infinity (which is obtained at 1/0) (thanks 1602 for the reminder)
NaNgetting is very simple: parseInt ("q") + 5; // parseInt - translates other types into a number
to check the variable for existence, you need to compare it with undefined
- if( undefined === someVar )
- {
- // если переменной нет, мы ее создаем
- var someVar = 1;
- }
* This source code was highlighted with Source Code Highlighter.
but there is one thing, but if JS has never heard anything about this variable, then there will be an error, the question immediately arises, what kind of nafik? And how to avoid this if you need to check a variable? You can avoid this as follows:
- if( undefined === window.someVar )
- {
- // все глобальные переменные – свойства “главного” объекта window
- // такой способ работает только с глобальными переменными
- var someVar = 1;
- }
* This source code was highlighted with Source Code Highlighter.
Finally, a couple of words about var : an example of how var can interfere with expectations:
- function makeSomeAction( someVariable )
- {
- for( q = 0; q < 3; q++ )
- {
- alert( someVariable );
- }
- }
-
- for( q = 0; q < 3; q++)
- {
- makeSomeAction( q );
- }
* This source code was highlighted with Source Code Highlighter.
many will expect the result: 000111222, but in fact the result will be 000 ... everything will change only var inside the function, which will separate the outer q from the inside q in the function,
this is the end of the first part, then I will talk a little about simple types (number, string, Boolean variables ), some ways of working with them, and about type casting