JavaScript Variables and Data Types
Variables give names to values so your JavaScript code can use and update data. Modern JavaScript mainly uses const and let to declare variables.
Use const for values you will not reassign
const siteName = "HTMLCSSCODING";
const pageCount = 4;
A const binding cannot be reassigned. If it refers to an object or array, the binding stays the same even though the object's contents may still be changed.
Use let for values that change
let score = 0;
score = score + 1;
let allows reassignment. Both let and const are block-scoped, which helps keep variables limited to the part of the code where they are needed.
Common JavaScript value types
- String: Text, such as
"hello". - Number: Numeric values, such as
42. - Boolean:
trueorfalse. - Array: An ordered list, such as
["HTML", "CSS"]. - Object: A group of named properties, such as
{ title: "Home" }.
Use descriptive names such as
totalPrice instead of short names such as x. Avoid var in new code unless you need to work with older JavaScript patterns.