Variables and data types
When it comes to programming in Javascript, understanding variables and data types is essential. These fundamental concepts form the building blocks of any software application.
Variables
In NodeJS, variables act as “containers” for storing data values. They provide a means to reference and manipulate information throughout your code. To declare a variable, you can use the var
, let
, or const
keywords.
var
: Used for declaring variables in older versions of JavaScript, but often avoided due to scope-related issues(⏭️).let
: Introduced to address the scoping problems ofvar
,let
defines variables with block-level scope, making them a better choice for most situations.const
: Similar tolet
, but once assigned a value, aconst
variable cannot be reassigned. It is ideal for values that shouldn’t change during the program’s execution.
Declaration
Declaration is the process of introducing a variable to your program. It involves specifying the variable’s name. In JavaScript and NodeJS, variables can be declared using the var
, let
, or const
keywords. We want to use let
or const
and not var
.
Variables in JS are case sensitive
console.log
is used for printing value in the console. As to why it has a dot(.
) and parenthesis(()
) we will cover it later(⏭️).
let playerName;
console.log(playerName); // undefined
Above, we declared a variable playerName
but it is empty or undefined
when we talk about JS.
Initialization
Initialization refers to the act of assigning an initial value to a variable when it’s declared. Proper initialization ensures that variables start with a meaningful value, minimizing unexpected behaviors and bugs in your code.
let counter = 0; // Initializing 'counter' with the value 0
let playerName = "Alice"; // Declaring and initializing 'playerName'
const maxScore = 100; // Declaring and initializing 'maxScore'
const MaxScore = 50;
// maxScore and MaxScore are two different variables (case sensitive)
console.log(playerName); // Alice
console.log(playerName, maxScore); // Alice 100
console.log(playerName, maxScore, MaxScore); // Alice 100 50
Data Types
NodeJS supports several data types, each serving a distinct purpose:
-
Primitive Data Types: These include numbers, strings, booleans,
null
, andundefined
. -
Complex Data Types: These are objects and functions.
-
Special Data Types: These consist of
Symbol
andBigInt
, introduced in later versions of JavaScript.
Let’s go through each of those:
Primitive Data Types
1. Numbers
Numbers in NodeJS can be integers or floating-point numbers.
let age = 25; // Integer
let temperature = 98.6; // Floating-point
2. Strings
Strings represent textual data and are enclosed in single or double quotes or backticks.
let name = "Alice";
let message = "Hello, NodeJS!";
let motivation = `This is fun`;
3. Booleans
Booleans represent two values: true
or false
, often used for conditional statements.
let isRaining = true;
let isLoggedIn = false;
4. null
and undefined
Both null
and undefined
represent the absence of a value, but they are used in different contexts. undefined
means that a variable exists, but it doesn’t have any value associated with it yet. null
represents the absence of a value in situations where the absence is intentional and has semantic meaning.
let emptyValue = null;
let notDefined = undefined;
Complex Data Types
1. Objects
Objects are collections of key-value pairs, allowing you to group related data together.
let person = { name: "Bob", age: 30 };
2. Arrays
Arrays store multiple values in a list.
let colors = ["red", "green", "blue"];
let numbers = [1, 2, 3, 4];
let mixed = ["a", 1, "f", 45];
Special Data Types
We’ll look into these more closely in intermediate tutorials
1. Symbol
Symbols are unique and often used as object property identifiers.
const idSymbol = Symbol("id");
2. BigInt
BigInt is used to represent large integers beyond the limits of the Number data type. It ends with an n
.
The Number.MAX_SAFE_INTEGER
static data property represents the maximum safe integer in JavaScript and it is (253 – 1).
const bigNumber = 1234567890123456789012345678901234567890n;
What now?
Now think of some everyday usecases and create variables from those. If nothing comes to mind here are a few to start with: shopping cart
, product in the store
, and even you
.