JavaScript Basic : The Language of the Web
Mastering Variables, Data Types, Operators, and Control Flow: The Building Blocks of JavaScript
Introduction
JavaScript a programming language, that was created to make the web page alive.
The programms written in JavaScript are called “Scripts”. It runs in the browser and works alongside HTML and CSS to create dynamic, user-friendly websites.
JavaScript was created by Brendan Eich in 1995. He was a developer at Netscape Communications Corporation.
Understanding JavaScript
What are Variables ?
Variables are containers in JavaScripts that are used to store different types of data.
You can declare variables using the var
, let
, or const
keywords:
Declaration of Variables
var
: Function-scoped or globally scoped. Can be re-declared and updated.let
: Block-scoped. Can be updated but not re-declared in the same scope.const
: Block-scoped. Cannot be updated or re-declared.
Example :
let box;
Now, we add some data inside the box.
box = 'Apples'; // store a string "Apple" in the box container
Only use
var
if you MUST support old browsers.A variable should be declared only once. A repeated declaration of the same variable is an error. ⚠️
Variable Naming Rules
Must begin with a letter,
$
, or_
.Case-sensitive.
Cannot use reserved keywords.
Data Types in JavaScript
JavaScript supports two types of Data Types :- Primitive Data Types and Non-Primitive Data types.
Primitive Data Types
Primitive data types represent a single value. They are immutable, meaning their values cannot be altered once created.
String
Represents textual data.
Enclosed in single (
'
), double ("
) or backticks (`
).Examples:
let name = "John"; let greeting = 'Hello'; let message = `Hi, ${name}`;
Number
Represents both integer and floating-point numbers.
Includes special values like
Infinity
,-Infinity
, andNaN
(Not-a-Number).Examples:
let age = 25; let price = 99.99; let result = 10 / 0; // Infinity let invalid = "hello" * 2; // NaN
BigInt
Used to represent integers larger than the
Number
type can hold.Created by appending
n
to an integer.Examples:
let bigNumber = 123456789012345678901234567890n;
Boolean
Represents a logical entity and has two possible values:
true
orfalse
.Examples:
let isActive = true; let isAdult = false;
Undefined
A variable declared but not assigned any value is
undefined
.Examples:
let x; console.log(x); // undefined
Null
Represents an intentional absence of a value or an empty value.
Examples:
let empty = null;
Symbol
Introduced in ES6, used to create unique identifiers.
Useful for creating unique object properties.
Examples:
let uniqueID = Symbol('id');
Non-Primitive Data Types
Non-primitive data types are objects and their subtypes. They are mutable, meaning their values can be altered.
Object
Represents a collection of key-value pairs.
Examples:
let person = { name: "Kumar", age: 20, isStudent: false, };
Array
- A special type of object used to store multiple values.
Examples:
let colors = ["red", "green", "blue"];
By using
typeof
operator we could easily find, to which data type the variable belongs.
Operators in Javascript
Category | Operator | Description |
Arithmetic | + , - , * , / , % , ** | Perform basic arithmetic operations (addition, subtraction, multiplication, division, modulus, exponentiation). |
Assignment | = , += , -= , *= , /= , %= | Assign and update values of variables. |
Comparison | == , != , === , !== , < , > , <= , >= | Compare two values. == compares values, === compares values and types. |
Logical | && , ` | |
Bitwise | & , ` | , ^ , ~ , << ,>> , >>> ` |
Unary | + , - , ++ , -- , typeof , delete , ! | Perform operations on a single operand (e.g., increment, decrement, check type, delete property, negate). |
Ternary | ? : | Acts as a shorthand for if...else . |
String | + | Concatenates strings. |
Type | typeof , instanceof | typeof checks data type. instanceof checks if an object is an instance of a specific class. |
Spread/Rest | ... | Expands elements of an array or collects arguments into an array. |
Destructuring | {} / [] | Extract values from objects or arrays. |
Nullish Coalescing | ?? | Returns the right-hand value if the left-hand value is null or undefined . |
Optional Chaining | ?. | Access object properties without throwing errors for null or undefined . |
Comma | , | Evaluates multiple expressions and returns the result of the last expression. |
Control Flow in JavaScript
Control flow statements determine the execution order of instructions in a program execute.
Conditional Statements
The Conditional Statements in JavaScript are :-
if
Statement
Executes a block of code if a condition is true.
if (x > 0) {
console.log("x is positive");
}
if-else
Statement
Executes one block of code if a condition is true, and another if it is false.
if (x > 0) {
console.log("x is positive");
} else {
console.log("x is not positive");
}
else if
Ladder
Allows multiple conditions to be checked.
if (x > 0) {
console.log("x is positive");
} else if (x < 0) {
console.log("x is negative");
} else {
console.log("x is zero");
}
switch
Statement
Used to perform different actions based on different conditions.
switch (fruit) {
case "Apple":
console.log("Apple selected");
break;
case "Banana":
console.log("Banana selected");
break;
default:
console.log("No fruit selected");
}
Conclusion
In conclusion, from this article, we got to know the importance of var
, let
, and const
in managing variable scope effectively. We also learned how conditional statements help in handling complex logic and making dynamic decisions. Mastering these basics forms the foundation for building clean, responsive, and maintainable JavaScript applications.
If you like this article subscribe to hashnode/Kumar-nirupam ——>
Want More—- ?
Follow —> Kumar Nirupam , Twitter/x