let person = "Hege";
let person="Hege";
first_name, last_name, master_card, inter_cityFirstName, LastName, MasterCard, InterCityfirstName, lastName, masterCard, interCity///* ... */var, let, or const.var keyword was used in all JavaScript code from 1995 to 2015. Use let and const for modern code.const if the value/type should not change.let only if const cannot be used.var only for older browser support.=) assigns values, e.g., x = x + 5.let x = 5 + 2 + 3;$ and _ as valid characters for variable names.let keyword was introduced in ES6 (2015).let have Block Scope.let must be declared before use.let cannot be redeclared in the same scope.let and const for Block Scope:
{
let x = 2;
}
// x cannot be used here
var have Global Scope.
{
var x = 2;
}
// x can be used here
let cannot be redeclared:let x = "John Doe";
let x = 0; // Error
var can be redeclared:var x = "John Doe";
var x = 0; // No Error
var can cause issues:
var x = 10; // x is 10
{
var x = 2; // x is 2
}
// x is 2
let solves this problem:
let x = 10; // x is 10
{
let x = 2; // x is 2
}
// x is 10
| Scope | Redeclare | Reassign | Hoisted | Binds this |
|---|---|---|---|---|
| var | Yes | Yes | Yes | Yes |
| let | No | No | Yes | No |
| const | No | No | No | No |
let and const have Block Scope.var does not have to be declared.var is hoisted.var binds to this.const was introduced in ES6 (2015).const cannot be:const has block scope.
const PI = 3.14159;
// PI = 3.14; // Error
// PI = PI + 10; // Error
const PI = 3.14159; // Correct
// const PI; // Incorrect
// PI = 3.14; // Error
const for:
const cars = ["Saab", "Volvo"];
cars[0] = "Toyota"; // Allowed
cars.push("Audi"); // Allowed
// cars = ["Ford"]; // Error
const car = {type: "Fiat"};
car.color = "red"; // Allowed
// car = {type: "Volvo"}; // Error
In JavaScript, primitive data types are the most basic types of data. They are immutable and are not objects. These include:
true or false values.null).Number type.
let a = null; // Null type
let b = 345; // Number type
let c = true; // Boolean type
let d = BigInt("456") + BigInt(3); // BigInt type
let e = "harry"; // String type
let f = Symbol("I am good"); // Symbol type
let g = undefined; // Undefined type
let h; // Will be undefined by default
console.log(a, b, c, d, e, f, g, h); // Outputs: null 345 true 459n 'harry' Symbol(I am good) undefined undefined
console.log(typeof d); // Outputs: 'bigint'
Objects in JavaScript are non-primitive data types used to store collections of data and more complex entities. Objects are key-value pairs and can be compared to dictionaries in Python.
const item = {
"array": true,
"rohn": 56
};
console.log(item["Harray"]); // This will return `undefined` because the key "Harray" does not exist in the `item` object.
b, c, d, etc.item object has keys "array" and "rohn" with their respective values.typeof operator helps identify the type of data stored in a variable.undefined.An expression is a fragment of code that produces a value. For example, 7; is an expression, as well as 7 + 5, which evaluates to 12. In this example, 7 and 5 are operands, and + is the operator. An operator works with one or more operands.
Every value that returns literally is an expression.
+: Addition-: Subtraction/: Division*: Multiplication**: Exponentiation%: Modulus (remainder)++: Increment--: Decrement++a: Pre-increment (increment, then print)--a: Pre-decrement (decrement, then print)a++: Post-increment (print, then increment)a--: Post-decrement (print, then decrement)=: Assignment+=: Add and assign-=: Subtract and assign*=: Multiply and assign/=: Divide and assign%=: Modulus and assign**=: Exponentiate and assign==: Equal to (checks value only)!=: Not equal to (checks value only)===: Strict equal to (checks value and type)!==: Strict not equal to (checks value and type)>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal tocondition ? expression1 : expression2
Used as a shorthand for if-else statements.
Logical operators are used to compare two or more conditions:
&&: Logical AND (returns true if both operands are true)||: Logical OR (returns true if at least one operand is true)!: Logical NOT (inverts the boolean value)