JavaScript Variables
A variable is referred to an identifier that holds a value assigned to it. A variable can be inserted in the program, running in this way with the value assigned to it.
On its own, a variable in JavaScript does not have information about the type of values that will be applied to it, which means that if you add a string to a variable, for instance, you can later add a number to it too. This operation will not result in an error in the program. That is the reason why JavaScript is also called an “untyped” language.
Yet before you use a variable, you should first declare it using the var or let keyword. If we are taking a constant, for example, the const keyword would be used. You can state a variable and assign it a certain value without having to use these keywords, but it is not advised to do so.
JavaScript Data types
As already mentioned, JavaScript is often called an “untyped” language, but this does not coincide with the real state of . It is true that you can add values of different types to variables, but at the same time there are certain data types in JavaScript. In particular, we are looking at primitive and object data types.
To define the data type of a certain value, you can activate the typeof operator. It returns a string illustrating the type of the operand.
Primitive data types
There are the following primitive JavaScript data types:
- number
- string
- boolean value
- special value null
- special value undefined
- symbol added in special cases, appeared in ES6
Object data types
To put it simply, other values that are not primitive have an assigned object type. We are specifically talking about functions, arrays, what we call “objects”, and many other components. All of these data types are based on the object type, and even if they differ in many functions, they still have a lot in common.
JavaScript Expressions
Expressions stand for code fragments that can be handled and get a certain value based on the computation performed. There are the following categories of expressions in JavaScript:
Arithmetic expressions
All of the expressions, whose calculation result in numbers, go to this category. To illustrate with an example:
1 / 2
i++
i -= 2
i * 2
String expressions
The results of processing such expressions are strings, similar to the following:
‘A ‘ + ‘string’
‘A ‘ += ‘string’
Primary expressions
This particular category is set for literals, constants, and references to identifiers:
2
0.02
‘something’
true
false
this / / execution context, reference to the current object
undefined
i [] {} [1,2,3] {a: 1, b: 2} {a: {b: 1}} Logical expressions utilize logical operators, and their processing result in logical values. For example: a && b a || b !a function() {} function(a, b) { return a * b } (a, b) = a * b a = a * 2da () => { return 2 } We have already discussed some objects above, mostly covered object literals, calling their methods, and accessing their properties. Now let’s talk about objects in more detail, in particular, we will review the mechanism of prototype inheritance as well as the class keyword. JavaScript is not like other modern programming languages because it has native support for prototypical inheritance, while other object-oriented languages use a class-based inheritance model instead. Thus, each JavaScript object has a special property (__proto__) that directs to another object that is its prototype. The object inherits the properties and features of the prototype. Let’s say we have an object built using an object literal: const car = {} Or we built an object using the object constructor: const car = new Object() In both these cases, the prototype of the car object will be Object. prototype.Array and object initialization expressions
Logical expressions
Expressions, function declarations
Operating with objects
Prototypical inheritance