You need to know basic JavaScript to use React or Next.js. You don't need to be a master of it to build cool web applications.
Here, I've shown the basic JavaScript concepts with examples. Just learn the concept of what is happening, and then you can search on Google to delve deeper into those particular concepts.
I am using Next.js App router here.
Const and Let Variables
In JavaScript, variables are used for storing data, and there are primarily two types of variable declarations: const and let.
const
The const keyword is used to declare variables whose values are intended to remain constant throughout their lifetime. Once assigned, the value of a const variable cannot be reassigned. if we try to reassign the value of a
a = 12 then we get this error : Cannot assign to 'a' because it is a constant.ts(2588)
let
The let keyword, on the other hand, is used to declare variables that are mutable, meaning their values can be reassigned.
like in the example the first value of b is 6, then we reassigned to 10, so the b value is 10 now, then we reassign b value to 20 , now the final b = value is 20
Data types
JavaScript supports a variety of data types, each serving different purposes in programming. These data types include:
- String: Represents textual data and is enclosed in double or single quotes.
- Number: Represents numeric values, including integers and floating-point numbers.
- Bigint: Represents integers of arbitrary length, useful for working with large numbers.
- Boolean: Represents logical values, either true or false.
- Undefined: Represents a variable that has been declared but has not been assigned any value.
- Null: Represents the intentional absence of any value.
- Symbol: Represents unique identifiers, useful for creating hidden or private object properties.
- Object: Represents complex data structures composed of key-value pairs, arrays, functions, and other objects.
The Object Datatype*
The object data type is versatile and can contain different kinds of data:
- An object: Represents a collection of properties where each property has a key-value pair.
- An array: Represents a sequential collection of elements, accessed by numeric indices, often used for lists of data.
- A date: Represents a specific point in time, enabling time-related operations and calculations.
Objects and Arrays and the difference
Objects:
- Definition: Objects are collections of key-value pairs, where each key is a string (or symbol) and each value can be any data type.
- Usage: Objects represent entities with named attributes, such as users, products, or any real-world concept.
- Accessing Values: Values in objects are accessed using their corresponding keys.
Arrays:
- Definition: Arrays are ordered lists of values, where each value can be any data type.
- Usage: Arrays store collections of related data, such as lists of items or sequential data.
- Accessing Values: Values in arrays are accessed using numeric indices, starting from 0.
- Note: Array is a type of object.
In summary, objects are used for storing named properties and represent entities with attributes, while arrays are used for storing ordered collections of values and manage lists of data.
Operators and Ternary Operators
Operators are symbols that perform operations on operands, such as variables or values. Ternary operators are a type of operator used for conditional expressions.
Operators:
Arithmetic Operators:
- Arithmetic operators perform mathematical operations on operands.
Comparison Operators:
- Comparison operators compare two values and return a boolean result.
Logical Operators:
- Logical operators combine two or more boolean expressions and return a single boolean result.
Ternary Operator:
- The ternary operator condition ? exprIfTrue : exprIfFalse is a shorthand for an if...else statement.
In summary, operators in JavaScript perform various operations on operands, and ternary operators provide a concise way to write conditional expressions. Understanding and using these operators effectively is essential for writing expressive and efficient JavaScript code.
If, Else Statements
In JavaScript, if and else statements are used for conditional execution, allowing your code to make decisions based on certain conditions.
if Statement:
- The if statement evaluates a condition inside parentheses. If the condition is true, the code block inside the curly braces {} is executed.
- If the condition is false, the code block is skipped.
else Statement:
- The else statement follows an if statement and executes a code block if the if condition is false.
- It doesn't have a condition of its own; it's simply a fallback option when the if condition is not met.
else if Statement:
- The else if statement allows you to specify additional conditions to test if the previous conditions are false.
- It follows an if statement or another else if statement and executes its code block if its condition is true.
Nested if Statements:
- You can nest if statements inside other if or else statements to handle more complex conditions.
In summary, if and else statements are fundamental for controlling the flow of your JavaScript code based on conditions, allowing for more dynamic and responsive behavior in your applications.