Skip to main content

Data types

@serverSerrverlesskiy

Dynamic typing

JavaScript is a loosely typed or dynamic language. This means that you do not need to define the type of the variable in advance.

Dinamics

The type will be determined automatically during program execution. It also means that you can use one variable to store different types of data :

Live Editor
Result
Loading...

typeof

In order to understand the data type contained in a variable, the typeof operator is used. The typeof operator returns the type of the argument. It has two syntaxes, with brackets and without:

  • Operator syntax: typeof x

  • Function syntax: typeof (x)

They work the same, but the first syntax is shorter.

The result of typeof is a string containing the type.

Live Editor
Result
Loading...

Data types

Data types

The JavaScript standard defines 9 data types. Get to know each of them by outputting to the console and then I will tell you about each of them in more detail.

let one = { firstName: 'John', lastName: 'Smith' } // object

let two = () => {} // function

let three = 'bar' // string

let four = 42 // number

let five = 19241924124n // bigint

let six = true // boolean

let seven = null // null

let eight // undefined

let nine = Symbol() // symbol

Objects

cupboard

In computer terminology, an object is a value in memory that can be referenced by an identifier. In JavaScript, an object can be thought of as a collection of properties. It's like a closet for storing other types of data.

Functions

Function

Functions⚙️ function are ordinary objects that have the additional ability to be called for execution.

Primitive values

Primitive

All data types in JavaScript, except for objects, are immutable (values cannot be modified, only overwritten with a new full value). For example, unlike the C language, where a string can be corrected character by character, in JavaScript the strings are re-created only completely. Values of these types are called "primitive values".

Text strings

text

In JavaScript, the type string is used to represent text data.

Numbers

Numbers

The numeric data type number represents both integer values and floating point numbers.

BigInt

giant

In JavaScript, the number type cannot contain numbers greater than (253-1) (i.e. 9007199254740991), or less than - (253-1) for negative numbers. This technical limitation is due to their internal representation.

For most cases, this is sufficient. But sometimes we need really gigantic numbers, like in cryptography or when using a "timestamp" with microseconds.

The bigInt type was added to JavaScript to allow for arbitrary length integers.

Boolean data type

The boolean type boolean represents a logical entity and has two 2️⃣ values: true

true

and false

False

This type is usually used to store yes / no values: true means “yes, right,” and false means “no, wrong”.

Null

Null

This data type has only one value: null. This value is specially designated as a primitive, since it is indeed a visible primitive in behavior. But at the same time, all other Objects are inherited from null, therefore, despite the fact that null returns a primitive value, its type is an object. For example, you can assign it to the default value.

Undefined

Unndefined

A variable that has not been assigned a value will have the value undefined.

Differences between null and undefined

Spiderman

null is the definite value of the absence of an object, while undefined denotes ambiguity. For example, you can check this in the browser console:

let TestVar
console.log(TestVar) // undefined
console.log(typeof TestVar) // undefined

null is an assignment value. It can be assigned to a variable as a representation with no value:

let TestVar = null
console.log(TestVar) // null
console.log(typeof TestVar) // object

It is clear from the previous examples that undefined and null are two 2️⃣ different types: undefined is the type itself (undefined), and null is an object.

null === undefined // false
null == undefined // true
null === null // true

Data type Symbol

Symbol

The Symbol type is a unique and immutable primitive value that can be used as a key for a property of an object. This type is so rarely used in real work that we will not even consider it in this course.

EnglishMoji!

Problems?

Problem

Write to Discord chat.

Questions:

Question

What is the typeof operator used for?

  1. To determine the data type
  2. To output the data type to the console
  3. To enter a new variable

How many data types does the JavaScript standard define?

1.7 2.9 3.5

What data type contains a set of properties?

  1. function
  2. string
  3. object

Can a string be adjusted character by character in JavaScript?

  1. Yes
  2. Possibly in special cases
  3. No, the lines are re-created only completely

What data type makes it possible to work with integers of arbitrary length?

  1. number
  2. string
  3. bigint

Which value is not of type boolean?

  1. false
  2. null
  3. true

Which data type has a value that can be used as a key for an object property?

1. undefined 2. symbol 3. null

In order to understand how much you learned this lesson, take the test in the mobile application of our school on this topic or in our telegram bot.

EnglishMoji!

  1. MDN web docs
  2. Code for Teens: The Perfect Beginner's Guide to Programming, Volume 1: Javascript - Jeremy Moritz
  3. JavaScript.ru

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Dmitriy Vasilev


Resoner2005

🐛 🎨 🖋