Variables, Datatypes and Operators, Conditional Statement, and Loops in JavaScript.

Rahul Kumar
8 min readAug 14, 2023

--

Identifiers:

Identifiers are those names that help in naming the elements in JavaScript.

Set of rules, you should follow to write an Identifiers:

  1. The first character of an identifier should be letters of the alphabet or an underscore (_) or dollar sign ($).
  2. You can use letters of alphabets or digits or underscores (_) or a dollar sign ($) to an Identifiers.
  3. Identifiers are case-sensitive. so, name and Name are not the same.

Note: You can’t use Reserved keywords as Identifiers.

Types of Identifiers (based on scope of the identifier and the data which an identifier will hold):

Data Types:

  • JavaScript language is a loosely typed or dynamically typed language.
  • The data is said to be primitive if it contains an individual value.

Number:

  • In JavaScript, any other value that does not belong to the above-mentioned types is not considered as a legal number. Such values are represented as NaN (Not-a-Number).

String:

  • String values are written in quotes, either single or double (like python).
  • To access any character within the string, it is important to be aware of its position in the string.
  • The first character exists at index 0, next at index 1, and so on.

Literals:

  • Literals can span multiple lines and interpolate expressions to include their results.

Boolean:

  • Boolean is a data type which represents only two values: true and false.
  • Example: 100, -5, “Cat”, 10<20, 1, 10*20+30, etc. evaluates to true whereas, 0, “”, NaN, undefined, null, etc. evaluates to false.

Undefined:

  • When the variable is used to store “no value”, primitive data type undefined is used.

null:

  • The null value represents “no object”.
  • Null data type is required as JavaScript variable intended to be assigned with the object at a later point in the program can be assigned null during the declaration.

Bigint:

  • BigInt is a special numeric type that provides support for integers of random length.
  • A BigInt is generated by appending n to the end of an integer literal or by calling the function BigInt that generates BigInt from strings, numbers, etc.

Symbol:

  • A “symbol” represents a unique identifier. You can make use of Symbol() to generate a value of this type.

Example:

  • Even if various symbols are created with the same description, they are different values. Thus, symbols ensures uniqueness. So the description provided can be considered as just a label.
  • Strings and symbols are basically different and should not accidentally get converted to the other one.
  • So far you know that symbols remain unique even if they have the same name. But at times, there may be a situation where you may want the symbols with same name to be same entities.

Then use global Symbols.

Non-Primitive Datatypes:

  • The data type is said to be non-primitive if it is a collection of multiple values.
  • JavaScript gives non-primitive data types named Object and Array, to implement this.

Objects:

Objects in JavaScript are a collection of properties and are represented in the form of [key-value pairs].

  • The ‘key’ of a property is a string or a symbol and should be a legal identifier.
  • The ‘value’ of a property can be any JavaScript value like Number, String, Boolean, or another object.

Syntax:

Example:

Array:

The Array is a special data structure that is used to store an ordered collection, which cannot be achieved using the objects.

  • How to declare an Array in JavaScript:
  • A single array can hold multiple values of different data types.

Operators:

  • Operators are categorized into unary, binary, and ternary based on the number of operands on which they operate in an expression.

typeOf :

  • “typeOf” is an operator in javaScript.

Example of typeOf:

👉 Statements:

Statements are instructions in JavaScript that have to be executed by a web browser.

JavaScript code is made up of a sequence of statements and is executed in the same order as they are written.

A Variable declaration is the simplest example of a JavaScript statement.

Other types of JavaScript statements include conditions/decision making, loops, etc.

⇨ White (blank) spaces in statements are ignored.

  • Non-Conditional statements are those statements that do not need any condition to control the program execution flow.

In JavaScript, it can be broadly classified into three categories as follows:

Comments:

// Single line Comment

/*
* Multi-Line
Comment
*/

Conditional Statements:

In JavaScript, there are three forms of the if...else statement.

  1. if statement
  2. if…else statement
  3. if…else if…else statement

if statement:

Syntax:

if (condition) {  // if condition is True then Body of If will run
// the body of if
}

Example:

let num = 5;
if(num > 3){
console.log("number is greater than 3");
}
console.log("Number is lesser than 3");

If…else statement:

Syntax:

if (condition) {
// the body of if
} else {
// body of else
}

Working of if…else statement:

Example:

let num = 2;
if(num % 2 == 0){
console.log( num," is Even Number" );
} else {
console.log(num," is Odd Number" );
}

Similarly, if….else…if will work.

Ternary operator:

  • It is also using as conditional statement.
  • ternary operator is the only conditional operator that takes three operands.

Example:

let num = 5;
(num % 2 == 0) ? console.log("Number is Even.") : console.log("Number is Odd.");

Switch-Case:

  • You can use a switch-case statement in place of if…else…if statement.
  • Switch-case does so more efficiently than repeated if…else…if statements.

Syntax:

switch (expression) {
case condition 1: statement(s)
break;

case condition 2: statement(s)
break;
...
...
...

case condition n: statement(s)
break;

default: statement(s)
}

Working Of Switch-case Statement:

Example:

let day = "monday"

switch (day) {
case "sunday":
console.log("It is Sunday today.");
break;
case "monday":
console.log("It is Monday today.");
break;
case "tuesday":
console.log("It is Tuesday today.");
break;
case "wednesday":
console.log("It is Wednesday today.");
break;
case "thursday":
console.log("It is Thursday today.");
break;
case "friday":
console.log("It is Friday today.");
break;
case "saturday":
console.log("It is Saturday today.");
break;
default:
console.log("It seems your day is not in Calender.")
}

/* output:
It is Monday today.
*/

Loop:

for loop:

  • “ for ”loop is used when the block of code is expected to execute for a specific number of times.

Example:

// print number from 1 to 10
for(let i = 1; i <= 10; i++){
console.log(i);
}

/* output:
1
2
3
4
5
6
7
8
9
10
*/

While loop:

  • ‘while’ loop is used when the block of code is to be executed as long as the specified condition is true.

example:

// print 1 to 10 
let num = 1; // intialization

while(num <= 10 ){ // condition
console.log(num);
num++; // increment (Update statement)
}

/* output:
1
2
3
4
5
6
7
8
9
10
*/

Do while:

  • ‘do-while’ is a variant of ‘while’ loop.
  • This will execute a block of code once before checking any condition.
// print 1 to 10
let num = 1; // intialization
do{
console.log(num);
num++; // update statement
}while(num <= 10); // condition

--

--

Rahul Kumar
Rahul Kumar

Written by Rahul Kumar

MERN Stack Developer building scalable web apps with MongoDB, Express.js, React, and Node.js. Sharing insights and best practices for modern web development.

No responses yet