Variable Declaration |
---|
Using Var,let,const |
Scope,Hoisting |
Re-declaration and Re-assignment |
var
, let
, and const
are keywords used for variable declaration in JavaScript. They differ in terms of scope, hoisting, and reassignment capabilities. Here's an explanation of each:
-
var
:- Variables declared with
var
are function-scoped or globally scoped, but not block-scoped. This means they are accessible throughout the entire function or globally if declared outside any function. - Variables declared with
var
are hoisted to the top of their scope, which means you can use them before they're declared, but their value will beundefined
. var
variables can be redeclared and reassigned.
function example() { var x = 10; if (true) { var x = 20; // This changes the value of the outer x as well } console.log(x); // 20 }
- Variables declared with
-
let
:- Variables declared with
let
are block-scoped, which means they're only accessible within the block they're defined in. let
variables are not hoisted, so you can't use them before they're declared.let
variables can be reassigned but not redeclared within the same scope.
function example() { let y = 30; if (true) { let y = 40; // This doesn't affect the outer y console.log(y); // 40 } console.log(y); // 30 }
- Variables declared with
-
const
:- Variables declared with
const
are block-scoped, likelet
. const
variables must be assigned a value at the time of declaration and cannot be reassigned.const
variables are not hoisted and have the same scoping rules aslet
.
function example() { const z = 50; if (true) { const z = 60; // This doesn't affect the outer z console.log(z); // 60 } console.log(z); // 50 }
- Variables declared with
For modern code, it's generally recommended to use let
and const
over var
. Use let
when you need to reassign a variable's value, and use const
when the value shouldn't change after assignment. This practice helps improve code readability and maintainability.
Primitive DataType | ->Are Immutable | ->Checks Value |
---|---|---|
Number | String | Boolean |
Undefined | Null | Symbol |
Primitive data types are the building blocks for more complex data structures and values in JavaScript. Unlike objects, primitive values are immutable, which means they cannot be changed directly; any operation on them creates a new value rather than modifying the existing one. Checks value not reference unlike non-primitive.
In JavaScript, there are six primitive data types:
-
Number: Can be int,float,negative,exponential, "NaN", "Infinity".
const age = -25; const price = -99.99; let scientificNum = 1.23e6; // 1.23 * 10^6 (1230000) let positiveInfinity = Infinity; console.log(positiveInfinity); // Infinity console.log(-10 / 0); // -Infinity console.log(1e308 * 2); // Infinity let result1 = 0 / 0; // NaN(Not a Number) let result2 = "abc" * 2; // NaN let result3 = Math.sqrt(-1); // NaN let nanResult = NaN * 10; // NaN let anotherNan = NaN + 5; // NaN
-
String: Represents sequences of characters enclosed in single ('') or double ("") quotes.
Strings are immutableconst name = "Alice"; const message = 'Hello, ' + name;
templating string:
console.log(`This template string by ${name}`); console.log(`${5+3}`);
-
Boolean: Represents true or false values.
const isStudent = true; const hasPermission = false;
-
Undefined: Represents a variable that has been declared but hasn't been assigned a value.
let x; // x is undefined
-
Null: Represents the intentional absence of any value or object.
const noValue = null;
-
Symbol: Represents a unique and immutable value, often used as object property keys.
const id = Symbol("unique identifier");
Type Conversions |
---|
-
String to Number:
let str = "42"; let num = Number(str); // Converts string to number 42
-
Number to String:
let num = 42; let str = String(num); // Converts number to string 42
-
Boolean to String:
let bool = true; let str = String(bool); // Converts boolean to string ("true" or "false")
-
String to Boolean:
let str = "true"; let bool = Boolean(str); // Converts string to boolean (true if string is not empty)
-
Number to Boolean:
let num = 0; let bool = Boolean(num); // Converts number to boolean (false if number is 0, true otherwise)
-
Object to String:
let obj = { key: "value" }; let str = String(obj); // Converts object to string "[object Object]"
-
String to Object:
let str = "{ key: 'value' }"; let obj = JSON.parse(str); // Converts JSON string to object
-
Number to Object:
let num = 42; let obj = new Number(num); // Converts number to Number object
-
Array to String:
let arr = [1, 2, 3]; let str = arr.toString(); // Converts array to comma-separated string "1,2,3"
-
String to Array:
let str = "1,2,3"; let arr = str.split(","); // Converts comma-separated string to array [1, 2, 3]
parseInt() | Number() |
---|
parseInt("123"); // Output: 123
parseInt("010"); // Output: 10 (interpreted as decimal, not octal)
parseInt("0xA"); // Output: 10 (interpreted as decimal)
parseInt("12.34"); // Output: 12 (fractional part is ignored)
Number("123"); // Output: 123
Number("010"); // Output: 10 (interpreted as decimal)
Number("0xA"); // Output: NaN (not a valid decimal number)
Number("12.34"); // Output: 12.34