- 3 Declaring Variables
- 4 Naming Variables
- 6 Primitive Data Types
- 7 Composite Data Types
- 8 Dynamic Typing
- 9 Variable Assignment and Reassignment
- 10 Assigning Values
- 11 Reassigning Values
- 12 Type Conversion
- 13 Implicit Type Conversion
- 14 Explicit Type Conversion
- 16 Final thoughts
- var: The traditional method of declaring variables uses “var.” Function scope refers to the fact that variables declared with the ‘var’ keyword are accessible for the duration of the function in which they are used.
- Let: The block-scoped variables can be declared using the ‘let’ function, which was added to ECMAScript 6 (ES6). The term “block scope” refers to a variable’s confinement to the block of code in which it is defined, such as a loop or a conditional statement.
- Const: Just like “let,” “const” has block scope, but it also has the added feature that once you assign a value to a “const” variable, you cannot change it. It is utilized for values that must stay constant throughout your program.
When naming variables, you should follow these rules:
- Since case matters, “myVar” and “myvar” are regarded as different variables.
- Variable names should begin with a letter, underscore (_), or dollar sign ($) and can be followed by letters, numbers, underscores, or dollar signs.
- Steer clear of naming your variable names with reserved terms like “if,” “for,” and “while.”
- Give your variables meaningful and precise names to make your code more legible.
Primitive Data Types
- Strings: Enclosed in single or double quotations, strings represent text. A string would be “Hello, World!” as an example.
- Number: Integers or floating-point values can be used as numbers. 42 or 3.14 are examples of numbers.
- Boolean: Booleans frequently represent true or false values in conditional statements. True or false are boolean examples.
- Undefined: Variables declared but not given a value are undefined. It stands for the lack of value.
- Null: The term null indicates the purposeful absence of any object value or a representation of no value or object.
- Symbol: Symbols are typically employed as object attributes to avoid name conflicts because they are distinct and immutable values.
Composite Data Types
- Object: Complex data structures called objects can store numerous key-value pairs. They are adaptable and used to represent real-world entities. For instance, an object can represent a person by having characteristics like name, age, and address.
- Array: An array is an organized collection of values. They are accessed by their index and used to store lists of data. For instance, [“apple,” “banana,” and “cherry”] is an array.
specifically. The interpreter uses the assigned value to determine the data type at runtime. While this flexibility has advantages, it must be handled carefully to avoid unforeseen behavior.
Variable Assignment and Reassignment
Different sorts of data, such as string, number, or boolean, can be assigned to variables as values:
- String: A variable can include a string value, such as a message or a name.
- Number: Variables used in calculations and mathematical procedures can have numerical values assigned to them.
- Boolean: Boolean variables can store true or false values, which are utilized in your code to make decisions.
You can change the values of variables declared with the ‘let’ and ‘var’ commands:
Let: Variables declared with ‘let’ can have their values changed at any time in your code.
However, variables that are declared with ‘const’ cannot be changed:
Const: Values that shouldn’t change while the program runs are stored as constants.
Implicit Type Conversion
Explicit Type Conversion
Utilizing built-in functions like “parseInt()” and “parseFloat(),” you can also do explicit type conversion to change texts into integers.