Comparing var, let, and const
There are three ways to declare variables in JavaScript. The var keyword has function scope, meaning the variable is accessible throughout the entire function when declared. On the other hand, let and const have block scope, meaning the variable is only accessible within the block where it was declared. There is also a difference between let and const: let allows the variable to be reassigned to a different value, while const does not allow reassignment. You can think of it in terms of whether the variable is immutable or not.
JavaScript Types
There are several data types provided in JavaScript, but they can be broadly divided into two categories. The first is Primitive types, and the second is Reference types.
- Primitive types: Boolean, String, Number, null, undefined, Symbol (ES6)
- Reference types: Object, Array, function, classes
Primitive types use the Call Stack to store values, while Reference types use Heap memory.
For Reference types, the size of the data is not fixed, the data values are stored in the heap, and the variable is assigned the address of the Heap memory.
Since JavaScript is a dynamically typed language, values can be changed at runtime. Therefore, you do not need to specify a data type when defining a variable.
const a = "asdf"; //string
const b = 123; // number
let c = 'foo';
c = 123123; // dynamic type
c = true;
c = Symbol(); // symbol type
c = ["hi", "hello"];
console.log(typeof c);
Quiz
Q1: What is the main topic covered in "Summary of Javascript Data Types"?
Summary of Javascript data types
Q2: What are the key takeaways from this article?
Summary of Javascript data types
Q3: How can the concepts in this article be applied in practice?
Consider the practical examples and patterns discussed throughout the post.