To see the actual code samples of this README guide, please check the 02-VariablesAndDataTypes github file.
Also Look At Variables.md
Also Look At DataTypes.md
Also Look At StaticallyVsDynamically.md
In this comprehensive README guide, we will explore the fundamental concepts of variables and data types in JavaScript. As a crucial part of any programming language, variables allow us to store and manipulate data, while data types define the kind of values that can be represented and operated upon within a program.
Variables are containers that hold values in computer programs. They provide a way to store and reference data, enabling us to write dynamic and reusable code. In JavaScript, variables can be declared using three different keywords: var
, let
, and const
.
var
The var
keyword was traditionally used to declare variables in JavaScript. However, it has some scoping issues and is now considered a legacy approach.
var age = 25;
let
The let
keyword was introduced in ECMAScript 6 (ES6) and is now the preferred way to declare variables that can be reassigned.
let name = "John Doe";
name = "Jane Smith"; // Allowed with let
const
The const
keyword is used to declare variables that are meant to be constants, meaning their values cannot be reassigned after initialization.
const PI = 3.14159;
PI = 3.14; // Error: Assignment to constant variable
When naming variables, it’s important to follow these rules:
_
), or dollar sign ($
).myVar
and myvar
are different variables).let
, const
, function
).let userName = "Alice"; // Valid
let user_name = "Bob"; // Valid
let 1stName = "Charlie"; // Invalid (starts with a digit)
let let = "David"; // Invalid (uses a reserved keyword)
You can assign a value to a variable using the assignment operator (=
). Updating the value of an existing variable is as simple as reassigning a new value to it.
let score = 100; // Initial assignment
score = 120; // Updating the value
JavaScript is a dynamically-typed language, meaning variables can hold values of different data types without explicit type declaration. The type of a variable is determined by the value assigned to it. JavaScript supports several data types, which can be categorized as primitive and non-primitive types.
Primitive data types in JavaScript are immutable, meaning their values cannot be changed once created. They are passed by value, which means a copy of the value is created in memory when assigned to a variable.
The string data type represents a sequence of characters enclosed in single quotes ('
), double quotes ("
), or backticks (`
).
const greeting = "Hello, world!";
const multilineString = `This
is a
multiline string`;
The number data type represents both integers and floating-point numbers.
const age = 25; // Integer
const temperature = 20.5; // Floating-point number
The boolean data type represents a logical value, either true
or false
.
const isLoggedIn = true;
const hasPermission = false;
The null
value represents an intentional non-value or an intentionally empty value.
let user = null; // No user object
The undefined
value is automatically assigned to variables that are declared but not initialized with a value.
let score;
console.log(score); // Output: undefined
Non-primitive data types in JavaScript are mutable, meaning their values can be changed after they are created. They are passed by reference, which means a reference to the value’s memory address is created when assigned to a variable.
The object data type is a collection of key-value pairs, where the keys are strings (or symbols), and the values can be of any data type, including other objects or functions.
const person = {
name: "John Doe",
age: 30,
isStudent: false,
hobbies: ["reading", "coding", "painting"],
};
An array is an ordered collection of values, each identified by a numeric index.
const numbers = [1, 2, 3, 4, 5];
const mixedArray = [true, "hello", 42, null, { key: "value" }];
Functions are a special type of object in JavaScript. They are first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned from functions.
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet("Alice"); // Output: Hello, Alice!
Understanding variables and data types is crucial for writing efficient and maintainable JavaScript code. By mastering these fundamental concepts, you’ll be well-equipped to tackle more advanced topics and build robust applications. Remember to always choose the appropriate data type for the task at hand and follow best practices for variable naming and declaration.
Happy coding!
To see the actual code samples of this README guide, please check the 02-VariablesAndDataTypes github file.
Back to Modern JavaScript Fundamentals