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

Identifiers:

Rahul Kumar
6 min readAug 14, 2023

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” 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:

Conditional Statements:

Ternary operator:

Example:

If-else:

It is similar to java programming.

Loop:

for loop:

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

While loop:

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

Do while:

  • ‘do-while’ is a variant of ‘while’ loop.
  • This will execute a block of code once before checking any condition.

Thanks..

--

--