Sitemap
Shraddha Dongol

Shraddha Dongol

JavaScript Essentials

A Beginner’s Guide to Variables, Datatypes, Operators & Functions

6 min readJan 31, 2025

--

The key to mastering JavaScript, or any programming language, is to take it step-by-step . We’ll begin by building a strong foundation, starting with the essentials like variables, functions, and operators, breaking down each concept one at a time.

Press enter or click to view image in full size

1. Variables

Imagine you have a box where you can put anything inside it like your favourite book, your clothes, your laptop and everything. In programming, a variable is like that box.

But instead of holding physical items, it holds data like integer (number), string (words), or Boolean (true/false) values.

  • Declaring and initializing a variable:
// A box for a number
let age; // Declaring a variable (an empty box)
age = 25; // Putting(initializing) the number 25 into the box

// A box for storing a word (string)
let name = "Shraddha"; // The name box holds the word "Shraddha"

// A box for storing a true/false value (Boolean)
let isLaptopOpened = true; // The isLaptopOpened box holds true

We can declare JavaScript in 3 ways : var, let, and const. They all hold values, but they differ in how and where they can be accessed.

var greeting = "Hello";
console.log(greeting); // Output: Hello

let age = 25;
console.log(age); // Output: 25

const city = "Kathmandu";
console.log(city); // Output: Kathmandu

// Reassigning let and var
greeting = "Hi";
console.log(greeting); // Output: Hi (Before reassign : Hello)
age = 30;
console.log(age); // Output: 30 (Before reassign : 25)

// Re-assigning const will throw an error
city = "Okhaldhunga"; // Error: Assignment to constant variable.

Re-declaration: var can be re-declared, but let and const cannot be re-declared in the same scope.

Re-assignment: You can re-assign values to var and let, but not to const.

2. Data Types

I. Primitive Data Types: Primitive data types are simple, predefined types in JavaScript. They are immutable, meaning their values cannot be changed once assigned, and any modification creates a new value.

  • String: A sequence of characters, like text.
  • Number: Represents numbers, including both whole numbers and decimals.
  • Boolean: Represents true or false values, often used for decision-making.
  • BigInt: Used for very large numbers that go beyond the usual number limit.
  • Undefined: A variable that has been declared but not assigned any value yet.
  • Null: Represents an empty or intentional absence of any value.
  • Symbol: A unique, unchangeable value often used as object keys.

let age = 20; // Number
let temperature = 37.5; // Float
let bigNumber = 1234567890123456789012345678901234567890n; // BigInt

let isActive = true; // Boolean

let x;
console.log(x); // Undefined

let data = null; // Null

let sym = Symbol('id'); // Symbol

II. Non-primitive Data Types: These are complex types, typically referred to as objects, and they can store multiple values. Unlike primitive data types, they are mutable, meaning their values can be modified after they are created.

  • Object: A collection of key-value pairs (properties) and methods (functions).
  • Array: A list-like collection of ordered elements.
  • Function: A block of reusable code that can be called with specific inputs (arguments).
  • Date: Represents a specific point in time.
  • RegExp: Represents a regular expression used for pattern matching in strings.
  • Map: A collection of key-value pairs where keys can be of any data type (not just strings or symbols).
  • Set: A collection of unique values, where duplicates are automatically removed.
let person = { name: "Alice", age: 20 }; // Object

let colors = ["red", "green", "blue"]; // Array

function greet() { // Function
console.log("Hello, World!");
}

let currentDate = new Date(); // Date

let pattern = /abc/; // RegExp

let map = new Map();
map.set("name", "Alice");
map.set(1, "one");
map.set(true, "boolean");

console.log(map); // Output: Map { 'name' => 'Alice', 1 => 'one', true => 'boolean' }

let uniqueNumbers = new Set([1, 2, 2, 3, 3, 4, 5, 5]); // Set
console.log(uniqueNumbers); // Output: Set { 1, 2, 3, 4, 5 } //removes duplicate values

When a primitive value is assigned to a new variable, a copy of the value is created.

When a non-primitive values are assigned to a new variable, the reference to the original object is copied, not the actual value.

3. Operators

Get Shraddha Dongol’s stories in your inbox

Join Medium for free to get updates from this writer.

JS operators are the special symbols that allows us to perform different operations like addition, multiplication and so on.

2 + 3;  // 5
// In this case, the '+' operator adds the two operands(2,3) and
// returns the result 5
  • Arithmetic Operators
Operator     Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Division Remainder)
** Exponentiation (Power)
++ Increment (increments by 1)
-- Decrement(decrements by 1)
  • Assignment Operators
Operator    Example       Same As
= x = y x = y //Assignment Operator
+= x += y x = x + y //Addition Assignment
-= x -= y x = x - y //Subtraction Assignment
*= x *= y x = x * y //Multiplication Assignment
/= x /= y x = x / y //Division Assignment
%= x %= y x = x % y //Remainder Assignment
**= x **= y x = x ** y //Exponentiation Assignment
  • Comparison Operators
Operator     Meaning 
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
=== Strictly equal to
!== Strictly not equal to
  • String Operators

The + operator in JavaScript can either perform arithmetic addition when used with numbers or string concatenation when one or both operands are strings.

// Number + Number = Number
let x = 4 + 4; //Output : 8

// String + Number = String
let y = "4" + 4; //Output : 44

let z = "Hello World" + 4; //Output : Hello World4
  • Logical Operators
Operator   Description
&& logical and
|| logical or
! logical not
let a = true;
let b = false;
let resultAND = a && b; // Output: false
let resultOR = a || b; // Output: true
  • Bitwise Operators
Operator   Description
& (AND) Compares each bit and returns 1 if both bits are 1.
| (OR) Compares each bit and returns 1 if at least one of the bits is 1.
^ (XOR) Returns 1 if the bits are different, otherwise 0.
let a = 5;  // Binary: 0101
let b = 3; // Binary: 0011
let result = a & b; // Output: 1 (Binary: 0001)
  • Ternary Operators

The ternary operator is a shortcut for the if-else statement.

condition ? value1 : value2;
- If the condition is true, it returns the first value (value1),
otherwise,
it returns the second value (value2)
let isTrue = true;
let message = isTrue ? 'This will be executed' : 'This will not be executed';
// Output: "This will be executed"
  • Type Operators
Operator      Description
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type
let num = 10;
console.log(typeof num); // Output: "number"

let str = "Hello";
console.log(str instanceof String); // Output: false (because str is a primitive string, not an object)

4. Functions

A function is a block of code that you can execute whenever needed. Functions make your code reusable, organized, and clean.

function function_name(parameter1, parameter2, parameter3) {
// code to be executed
}

- A JavaScript function is defined with the function keyword,
- Followed by name of the function, then
- Followed by a list of parameters, enclosed in parentheses and separated by commas.(parameter1, parameter2, ...)
- The code to be executed is placed inside curly brackets: {}
//Defining a function
function square(number) {
return number * number;
}

//Calling or Invoking a function
square(5); // Output : 25

JavaScript may seem vast and overwhelming at first, but remember — every expert was once a beginner. As I mentioned earlier, the key to mastering JavaScript is to take it step-by-step.

Once you’re comfortable with these fundamental concepts, you can slowly explore the endless possibilities that JavaScript offers, including powerful frameworks and libraries like React, Vue.js, and Node.js. It’s no surprise that Statista declared JavaScript as one of the most powerful programming languages worldwide in 2025, with 66% of developers using it. This statistic shows just how impactful learning JavaScript can be for your coding journey and career.

Don’t rush. Take your time. Explore.
Each line of code you write, every error you debug, and all the small wins you achieve along the way will add up.

So let’s start this journey together, and soon enough, we’ll reach new heights in JavaScript development!

--

--