A collection of JavaScript practice programs and concepts to improve coding skills through daily exercises.
JavaScript operators are special symbols or keywords that perform operations on values or variables. They are essential for creating expressions that compute values or make decisions in the code.
-
Arithmetic Operators are used to perform basic mathematical calculations such as addition, subtraction, multiplication, division, finding the remainder (modulus), and increasing or decreasing a value by one. These allow you to work with numbers easily.
-
Assignment Operators assign values to variables. The basic assignment operator = sets a variable to a value. There are also combined assignment operators like +=, -=, *=, and /= which update the variable’s value by performing an arithmetic operation and then assigning the result back.
-
Comparison Operators compare two values and return either true or false. They help in making decisions by checking if values are equal, not equal, greater than, less than, or meet other comparison criteria. The strict equality operator (===) checks both value and type, while == checks only value.
-
Logical Operators are used to combine or invert boolean values (true or false). They include “AND” (&&), “OR” (||), and “NOT” (!). Logical operators are commonly used in conditional statements to control the flow of the program based on multiple conditions.
Together, these operators provide a fundamental toolkit for performing calculations, assigning values, comparing data, and controlling program logic in JavaScript.
Type conversion in JavaScript is the process of changing a value from one data type to another. It can happen in two ways:
-
Implicit Conversion (Type Coercion): JavaScript automatically converts types when performing operations involving different data types. For example, when a number and a string are combined, the number is converted to a string.
-
Explicit Conversion: The programmer manually converts a value from one type to another using built-in functions like String(), Number(), and Boolean().
Type conversion allows JavaScript to work flexibly with different data types, enabling operations and comparisons. Understanding how and when conversions happen is important to avoid unexpected results in code.
Control statements in JavaScript are used to manage the flow of program execution based on specific conditions or requirements. By using control statements, JavaScript enables programs to make decisions, repeat tasks, and alter execution order—making code more interactive, adaptable, and efficient. What Are Control Statements? Control statements determine whether certain blocks of code should execute and, if so, how many times or under what conditions. They are essential for programming logic and help structure tasks such as decision-making, looping, and early exit from loops or functions.
JavaScript control statements can be grouped into three main categories:
These allow a program to take different actions depending on whether a condition is true or false:
- if:- Executes a code block if a specified condition is true.
- if...else:- Executes one block if the condition is true, otherwise executes another.
- if...else if...else:- Chains multiple conditions for more complex decision-making.
- nested if...else statement:- An if or else block placed within another if or else block for multilayered decisions.
- switch:- Offers multiple paths based on the value of an expression.
- ternary operator (?:):- A shorthand conditional statement for simple choices.
Used to repeat blocks of code until a condition is met:
- for loop:- Runs a block for a specified number of times.
- while loop:- Repeats as long as a condition is true.
- do...while loop:- Executes at least once, then repeats while a condition remains true.
These change the normal sequence of execution:
- break:- Exits a loop or switch early.
- continue:- Skips the current loop iteration and moves to the next.