Variables. Primitive data types. Comments. Constants. The keywords var, const. Operator typeof
- 1. Variables and data types. General concepts
- 2. Variable declaration. Keyword var. Basic (primitive) data types
- 3. Variable name requirements
- 4. Examples of declaring variables of different types
- 5. Using variables in the program. What does the term “not strongly typed language” mean? Example
- 6. The typeof operator. Get the type of a variable
- 9. Declaring constants. The const keyword
- Related topics
Search other resources:
1. Variables and data types. General concepts
- the variable has a name. By this name, you can access the data stored in the variable. There are certain requirements for variable names (see below);
- the variable has a specific data type.
The data type of the variable determines:
- a way of representing (encoding) data in memory. Variables of different types are encoded differently;
- the size of the memory in bytes that the variable occupies. For example, string and numeric types take up different amounts of memory in a program.
2. Variable declaration. Keyword var. Basic (primitive) data types
The var keyword is used when declaring a variable. A variable declaration can be:
- with initial initialization. The term “initialization” means that a variable is assigned its original value using an assignment operation;
- without initial initialization. In this case, nothing is assigned to the variable. Such a variable is set to type undefined by default.
According to the two ways of declaring a variable, the following two general forms are distinguished.
If, when declaring a variable, it needs to be initialized, the following general form is used
var variableName = value;
- variableName – the name of the variable being declared;
- value – the value that the variable receives when it is declared. The type of this variable will be determined based on the value. If value is a string, the variable will be of type String, if value is a number, then the variable will be of type Number, etc.
After initialization, variables can receive values of the following basic (primitive, simple) data types:
- Number is a data type describing numbers. There is no strict separation between integers and floating point numbers as in other programming languages;
- Boolean – boolean data type. Variables of this type receive only two possible values: true or false;
- Null – a type that can take only one null value;
- Undefined – the type set when the variable is declared (var) but not initialized with a value. The value of the variable will be assigned later;
- Bigint – a type designed to represent large integers whose values go far beyond the value of the Number type;
- Symbol – defines a unique immutable primitive value (atom).
There are also object types that describe objects, functions, arrays, errors, regular expressions, date and other program elements. These types are not the subject of this topic.
If a variable is declared without initial initialization, then the general form of the declaration is as follows:
- variableName is the name of the variable being declared. This variable is set to type undefined, which means an undefined data type.
Direct assignment of undefined value for a variable is allowed
var variableName = undefined;
In an application, variable declarations can be on the same line. However, these declarations are separated by a comma.
var varName1, varName2, varName3;
3. Variable name requirements
JavaSctipt has the following requirements for variable names or other program identifiers:
- variable (identifier) name can contain uppercase (‘A’ … ‘Z’) and lowercase (‘a’ … ‘z’) letters, digits ‘0’ … ‘9’ and underscore character ‘_’;
- a variable name cannot start with a digit;
- the variable name can contain the ‘$’ symbol.
abc Abc aBC ABC
are different names.
4. Examples of declaring variables of different types
Valid variable declarations
// Variable declarations of different typesvar x = 2.88; // Number type var MAX_VALUE = 1000; // Number type var b = false; // Boolean type var z; // z = Undefined (Undefined type) var Arr = null; // Object type // Declaring multiple String variables in one line var Text = "Hello, world!", Url = "www.bestprog.net"; // Declaring multiple variables of type Number on one line var $Pi = 3.1415, G = 9.806, $E = 2.718; // Declaring variables of Bigint type var Nm = 9007199254740992n; var Nm2 = 20n; // Symbol type var sm = Symbol("abc")
Incorrect variable declarations
// Incorrect variable declarations var 1f = 25; // the name begins with a number var f 3 = 28; // space character between f and 3
5. Using variables in the program. What does the term “not strongly typed language” mean? Example
'use strict' // 1. Declaring variable a var a; // The variable type is undefined // 2. The variable a is assigned a number (integer) a = 110; // The variable type is number // 3. The variable a is assigned a real number a = 2.55; // The number type // 4. The variable a is assigned a string a = "abcd"; // The string type // 5. Variable a is set to null a = null; // The object type // 6. The variable a is assigned the boolean type a = true;
6. The typeof operator. Get the type of a variable
Using the typeof operator, you can get the name of the type that is set for a variable. The general form of calling the typeof operator is as follows
- value – some variable, literal or expression.
Example. The example defines the types of variables, literals, and expressions using the typeof operator.
// The typeof operator var a = 20; var tp_a = typeof(a); // tp_a is of type number var str = 'Hello'; var tp_str = typeof(str); // the String type var tp_expr = typeof(5 + true + 2.8) // the Number type alert(tp_expr) tp_expr = typeof('A' + 8) // String type alert(tp_expr)
In any application, it is advisable to add explanations to code fragments in the form of comments. The use of comments makes it easier to understand the components of the program and their further maintenance. Comments are not interpreted by the compiler or interpreter as code to be processed. Comments are simply ignored.
- one-line – placed behind a pair of characters //;
- multi-line – placed between the pairs of characters /* and */.
// This is a one-line comment. // var a = 20; - this is also a comment var a = 20; // the declaration of the variable a is a comment /* This is a multi-line comment */
abstract else instanceof switch boolean enum int synchronized break export interface this byte extends long throw case false native throws catch final new transient char finally null true class float package try const for private typeof continue function protected var debugger goto public void default if return volatile delete implements short while do import static with double in super
If you try to use one of the above names in the program, it will be considered as error.
9. Declaring constants. The const keyword
const constName = value;
- constName – the name of constant;
- value – the value returned by the constant.
After declaring a constant constName, an attempt is made to change the value of the constant
constName = anyValue;
will be considered an error. It is forbidden to change any constant before the end of the script.
The names for the formation of constants are the same as for variables (see paragraph 5).
// Constants. Declaration and use of constants const Pi = 3.1415; // constant type Number const Yes = true, No = false; // Boolean const Numbers = "0123456789"; // String const N_Months = 12; // Number const MaxYearsOfUniverse = 123456789012345678901234567890n // Bigint