In JavaScript, variables are used to store data values. You can think of a variable as a container that holds a value, and this value can be changed or updated during the execution of the program. Variables in JavaScript are dynamically typed, meaning you can assign values of different types to the same variable.
Variable Declaration:
You can declare a variable in JavaScript using the var, let, or const keywords.
var: Historically used for variable declaration, but it has some quirks and is less commonly used nowadays.
let: Introduced in ECMAScript 6 (ES6), it allows block-scoped variables that can be reassigned.
const: Also introduced in ES6, it declares constants (variables whose value cannot be changed once assigned) and are block-scoped.
Syntax:
// Using var (less common nowadays)
var myVar = "Hello";
// Using let (preferred for mutable variables)
let myVar = "Hello";
// Using const (for variables that should not be reassigned)
const myVar = "Hello";
Variable Naming Rules:
Variable names must begin with a letter, $, or _.
Subsequent characters can be letters, digits, $, or _.
Variable names are case-sensitive (myVar, myvar, and MYVAR are different variables).
Data Types:
JavaScript variables can hold various types of data:
Primitive Data Types:
Number: Integers, floating-point numbers, etc.
String: Textual data enclosed in quotes ('single' or "double").
Boolean: true or false.
Null: Represents the intentional absence of any object value.
Undefined: Represents the uninitialized state of a variable.
Symbol (ES6): Unique and immutable data type introduced in ES6.
Complex Data Types:
Object: A collection of key-value pairs.
Array: An ordered list of values.
Function: A reusable block of code.
Example:
let age = 25; // Number
let name = "John"; // String
let isStudent = true; // Boolean
let person = null; // Null
let job; // Undefined
const PI = 3.14; // Constant
let personObj = { name: "Alice", age: 30 }; // Object
let numbersArray = [1, 2, 3, 4, 5]; // Array
function greet() { return "Hello!"; } // Function
Variable Scope:
Global Scope: Variables declared outside of any function have global scope, meaning they can be accessed from anywhere in the code.
Local Scope: Variables declared within a function have local scope, meaning they are only accessible within that function.
Hoisting:
In JavaScript, variable declarations are hoisted to the top of their containing scope during the compilation phase. However, only the declaration is hoisted, not the initialization.
Chapter :
00:00 Introduction
00:22 Variable Declaration
00:54 Syntax
01:03 Variable Naming Rules
01:21 Various Data Types
01:34 Variable Scope
02:50 Hoisting
02:02 Best Practices
02:19 Summary
Thank you
EVERYDAY BE CODING
javascript
Copy code
console.log(x); // Output: undefined
var x = 5;
Best Practices:
Use let or const instead of var to declare variables.
Use meaningful variable names to enhance code readability.
Declare variables close to where they are used (preferably at the beginning of the scope).
Use const for variables that should not be reassigned.
Thank you
EVERYDAY BE CODING
On this page of the site you can watch the video online Variables and Data Types in JavaScript - #4 with a duration of hours minute second in good quality, which was uploaded by the user Everyday Be Coding 17 March 2024, share the link with friends and acquaintances, this video has already been watched 67 times on youtube and it was liked by 0 viewers. Enjoy your viewing!