| Variable Declaration |
|---|
| Using Var,let,const |
| Scope,Hoisting |
| Re-declaration and Re-assignment |
var,let, andconst 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.
functionexample(){varx=10;if(true){varx=20;// This changes the value of the outer x as well}console.log(x);// 20}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.
functionexample(){lety=30;if(true){lety=40;// This doesn't affect the outer yconsole.log(y);// 40}console.log(y);// 30}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.
functionexample(){constz=50;if(true){constz=60;// This doesn't affect the outer zconsole.log(z);// 60}console.log(z);// 50}
For modern code, it's generally recommended to uselet andconst overvar. Uselet when you need to reassign a variable's value, and useconst 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".
constage=-25;constprice=-99.99;letscientificNum=1.23e6;// 1.23 * 10^6 (1230000)letpositiveInfinity=Infinity;console.log(positiveInfinity);// Infinityconsole.log(-10/0);// -Infinityconsole.log(1e308*2);// Infinityletresult1=0/0;// NaN(Not a Number)letresult2="abc"*2;// NaNletresult3=Math.sqrt(-1);// NaNletnanResult=NaN*10;// NaNletanotherNan=NaN+5;// NaN
String:Represents sequences of characters enclosed in single ('') or double ("") quotes.
Strings are immutable
constname="Alice";constmessage='Hello, '+name;
templating string:
console.log(`This template string by${name}`);console.log(`${5+3}`);Boolean:Represents true or false values.
constisStudent=true;consthasPermission=false;
Undefined:Represents a variable that has been declared but hasn't been assigned a value.
Null:Represents the intentional absence of any value or object.
Symbol:Represents a unique and immutable value, often used as object property keys.
constid=Symbol("unique identifier");
String to Number:
letstr="42";letnum=Number(str);// Converts string to number 42
Number to String:
letnum=42;letstr=String(num);// Converts number to string 42
Boolean to String:
letbool=true;letstr=String(bool);// Converts boolean to string ("true" or "false")String to Boolean:
letstr="true";letbool=Boolean(str);// Converts string to boolean (true if string is not empty)
Number to Boolean:
letnum=0;letbool=Boolean(num);// Converts number to boolean (false if number is 0, true otherwise)
Object to String:
letobj={key:"value"};letstr=String(obj);// Converts object to string "[object Object]"String to Object:
letstr="{ key: 'value' }";letobj=JSON.parse(str);// Converts JSON string to objectNumber to Object:
letnum=42;letobj=newNumber(num);// Converts number to Number object
Array to String:
letarr=[1,2,3];letstr=arr.toString();// Converts array to comma-separated string "1,2,3"
String to Array:
letstr="1,2,3";letarr=str.split(",");// Converts comma-separated string to array [1, 2, 3]
parseInt("123");// Output: 123parseInt("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: 123Number("010");// Output: 10 (interpreted as decimal)Number("0xA");// Output: NaN (not a valid decimal number)Number("12.34");// Output: 12.34