JavaScript: Variables, Data Types, Expressions and Objects

JavaScript Variables, Data Types, Expressions and Objects

Variables

A variable is an identifier that has a value assigned to it. A variable can be accessed in the program, working in this way with the value assigned to it.

By itself, a variable in JavaScript does not contain information about the type of values that will be stored in it, which means that if you write a string to a variable, for example, you can later write a number to it. This operation will not cause an error in the program. That is why JavaScript is sometimes called an “untyped” language.

Before you use a variable, you must declare it using the var or let keyword. If we are talking about a constant, the const keyword is used. You can declare a variable and assign it a certain value without using these keywords, but it is not recommended to do so.

Data types

JavaScript is sometimes called an “untyped” language, but this does not correspond to the real state of affairs. It is true that you can write values of different types to variables, but there are data types in JavaScript. In particular, we are talking about primitive and object data types.

To determine the data type of a certain value, you can use the typeof operator. It returns a string indicating the type of the operand.

Primitive data types

Here is a list of primitive JavaScript data types:

  • number (number)
  • string (string)
  • boolean (boolean value)
  • null (special value null)
  • undefined (special value undefined)
  • symbol (symbol used in special cases, appeared in ES6)

Objects

All values that are not primitive have an object type. We are talking about functions, arrays, what we call “objects”, and many other entities. All of these data types are based on the object type, and although they differ in many ways, they also have a lot in common.

Expressions

Expressions are code fragments that can be processed and get a certain value based on the calculations performed. There are several categories of expressions in JavaScript.

Arithmetic expressions

Expressions, whose calculation results in numbers, fall into this category.

1 / 2

i++

i -= 2

i * 2

String expressions

The results of evaluating such expressions are strings.

‘A ‘ + ‘string’

‘A ‘ += ‘string’

Primary expressions

This category includes literals, constants, and references to identifiers.

2

0.02

‘something’

true

false

this / / execution context, reference to the current object

undefined

i //where i is a variable or constant

Array and object initialization expressions

[] //array literal

{} //object literal

[1,2,3]

{a: 1, b: 2}

{a: {b: 1}}

Logical expressions

Logical expressions use logical operators, and their calculation results in logical values.

a && b

a || b

!a

Expressions, function declarations

function() {}

function(a, b) { return a * b }

(a, b) = a * b

a = a * 2da

() => { return 2 }

Working with objects

We have already dealt with objects above, talking about object literals, calling their methods, and accessing their properties. Here we will talk about objects in more detail, in particular, we will consider the mechanism of prototype inheritance and the use of the class keyword.

Prototypical inheritance

JavaScript stands out among modern programming languages because it supports prototypical inheritance. Most object-oriented languages use a class-based inheritance model.

Each JavaScript object has a special property (__proto__) that points to another object that is its prototype. The object inherits the properties and methods of the prototype.

Let’s suppose that we have an object created using an object literal.

const car = {}

Or we created an object using the object constructor.

const car = new Object()

In any of these cases, the prototype of the car object will be Object. prototype.

Recommended Articles

Share
Tweet
Pin
Share
Share