Types, values, and variables in JavaScript

Subscribe to my newsletter and never miss my upcoming articles

In this article, we will take an overview of types, values, and variables in JavaScript.

A compuer program can simply be explained as a piece of code that manipulates something.

So what is something?

Let’s ask the computer to perform some task, Hey, computer print “Hello Devs” 2 times.

So, in the above statement, there are two entities,

  1. “Hello Devs”
  2. 2

These will be the values used by the computer program. First is a set of characters and the second is digit/number these are called types.

Ok, what if we want these values later in our program? Let’s save values in some container and name that container as abc. This container is called a variable.

JavaScript types mainly can be divided into two categories:-

  1. Primitive types
  2. Object type

Primitive types include numbers, strings of text, and boolean values(true/false). The special type of values like null and undefined are primitive values, but they are not numbers, strings, or boolean. ES6 added a new special-purpose type, known as Symbol.

Any value that is not a primitive value(number, string, boolean, symbol, null, or undefined) is an Object.

An object is a collection of properties where each property has a name and value pair. The values of an object can be a primitive value or another object.

JavaScript automatically converts values from one type to another. If the program expects a string and you provided a number, it will automatically convert the number to a string.

Numbers

The number is used to represent integers. JavaScript represents numbers using a 64-bit floating-point format defined by IEEE 754 standard.

This means it can represent numbers as large as +/- 1.797693134862315710^308 and as small as +/- 510^-324.

If we use integer values larger than the range, we may lose precision in trailing digits.

If a number appears directly in a JavaScript program it is called numeric literals. I will explain Numbers in detail in an upcoming article.

Text

To represent text in our program JavaScript provides type as a String.
A String is an immutable ordered sequence of 16-bit values. Each 16-bit value represents a Unicode character.
The length is the number of 16-bit values that are used to represent a string. JavaScript strings use zero-based indexing, the first 16-bit value is placed at 0th index and 2nd at 1st index, and so on.

You can find details about strings in javascript in upcoming articles.

Boolean values

null is a reserved keyword to represent the absence of the value.
Using typeof operator on null returns type as an “object”, indicating that null can be used as a special value that indicates “no object”.
Other programming languages also have equivalent Javascript null: like NULL, nil, or None.

The undefined value represents a deeper kind of absence. It is the value of the variable that not has been initialized.
Many times we see this value when we try to get the value of an object property or array element which does not exist.

undefined is a predefined global constant(not language keyword as null) that is initialized to an undefined value.

If we try to apply typeof operator to an undefined value it returns “undefined”, indicating that this is a member of a special type.

Symbols

Symbols were introduced in ES6 to use non-string property names.
JavaScript Object types are an unordered collection of properties, where each property has a name and value.
Before ES6 property names are typically a string.

To obtain symbol values, we need to call the Symbol() function. This function never returns the same value twice even we call it with the same argument.

Symbol.for()- This method allows us to create the same symbol value twice. Passing the same string argument to Symbol.for() method returns the same symbol value. Symbol.keyFor() returns the string that we passed as an argument to Symbol.for().

let var1 = Symbol.for(“test”);
let var2 = Symbol.for(“test”);
va1 === var2              // true

Variable Declaration and Assignment

In the programming language, we use names/identifiers to represent values.
Binding name to value gives us a way to refer to that value ad use it in the programs we write.
By doing this we can say that we are assigning value to a variable.

The term variable implies that a new value can be assigned: the value associated with the variable may vary as our program runs.
If we permanently assign some value to a name, that name we refer to as constant instead of variable.

Variable and scope

The scope of a variable is the region of our program source code in which it is defined.
Variable and constant declared with let and const are blocked scope. This means the variable is only accessible inside the code block where let or const exists.

var as a global variable

If we declare the global variable using the var keyword it is a part of the global object and can be referred to as globalThis.
Global declared with var can not be deleted using the delete keyword.
Variable declared with let and const are not a part of globalThis.

This is an overview of Javascript types and variables.
In the next article from this series, I will cover the Number data type in detail.

Hope you like it, if yes **like & share.**

Thanks for your time.

Happy coding….

← How does JavaScript work? 🤔 Numbers in JavaScript →

No Comments Yet