Posted January 23Jan 23 You are reading Part 2 of the 39-part series: JavaScript Skill Progression: The Path from Beginner to Extreme. [Level 1]IntroductionVariables are fundamental to programming, allowing developers to store, manipulate, and retrieve data dynamically. JavaScript uses variables to hold values that can be used throughout the script. Understanding how variables work and the different data types available is essential for writing efficient and error-free code.Understanding JavaScript variables and data types is crucial for writing robust applications. With a proper grasp of primitive and non-primitive data types, developers can manipulate data efficiently while avoiding common pitfalls like unintended type coercion. As JavaScript continues to evolve, mastering variable scoping and type handling will improve the quality and maintainability of your code.Declaring Variables in JavaScriptJavaScript provides three keywords for declaring variables:var – The oldest way to declare variables, now largely replaced by let and const due to scoping issues.let – Allows block-scoped variable declarations.const – Used for declaring constants, meaning their value cannot be reassigned.Example:var oldVar = "This is a var variable"; let newVar = "This is a let variable"; const constantVar = "This value cannot be changed"; JavaScript Data TypesJavaScript has two main categories of data types:1. Primitive Data TypesPrimitive data types are immutable and store single values.String – A sequence of characters. let text = "Hello, World!"; Number – Represents both integer and floating-point numbers. let num = 42; Boolean – Represents true or false values. let isJavaScriptFun = true; Undefined – A variable that has been declared but not assigned a value. let noValue; console.log(noValue); // undefined Null – Represents an intentional absence of a value. let emptyValue = null; Symbol – A unique and immutable identifier (introduced in ES6). let uniqueId = Symbol("id"); BigInt – Used for working with very large numbers beyond the Number type limit. let bigNumber = BigInt(9007199254740991); 2. Non-Primitive (Reference) Data TypesReference data types hold objects and collections of data.Object – A collection of key-value pairs. let person = { name: "John Doe", age: 30 }; Array – A list-like collection of values. let fruits = ["Apple", "Banana", "Cherry"]; Function – A reusable block of code. function greet(name) { return "Hello, " + name; } Type Checking in JavaScriptJavaScript provides the typeof operator to check the type of a variable:console.log(typeof 42); // "number" console.log(typeof "Hello"); // "string" console.log(typeof true); // "boolean" console.log(typeof undefined); // "undefined" console.log(typeof null); // "object" (a known JavaScript quirk) console.log(typeof Symbol("id")); // "symbol" console.log(typeof BigInt(9007199254740991)); // "bigint" console.log(typeof { name: "John" }); // "object" console.log(typeof [1, 2, 3]); // "object" (arrays are a type of object) console.log(typeof function() {}); // "function" You are reading Part 2 of the 39-part series: JavaScript Skill Progression: The Path from Beginner to Extreme. [Level 1]
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.