Primitives โ
Primitives are the basic building blocks in JavaScript, think of primitives as the simple Lego blocks. Without them, you can't build your dream Death Star! ๐. It has 7 primitives
Types โ
1. String: Not Just for Text Messages ๐ฑ โ
typeofย return value: string.
Strings represent text and are created using single quotes ('Hello'), double quotes ("Hello"), or backticks (`Hello`).
Tip: With backticks, you can embed expressions using ${expression}.
Common Methods โ
length: Returns the length of the string.toUpperCase(): Converts the string to uppercase.includes(): Checks if a substring is present.- More Methods
2. Number: More Than Just Math ๐งฎ โ
typeofย return value: number.
JavaScript has a single number type, which can be an integer or a floating-point number. It also includes special values like NaN (Not-a-Number) and Infinity, because sometimes math just needs a little drama.
Common Methods:
toFixed(): Formats a number using fixed-point notation, so your floating points stop floating around aimlessly.toString(): Converts a number to a string, because even numbers need to express themselves.parseInt(): Parses a string and returns an integer, making sense of the nonsense your users might input.- More Methods
3. Boolean: True or False, No Middle Ground ๐ฆ โ
typeofย return value: boolean.
Booleans represent one of two values: true or false. They are the digital equivalent of yes or no, on or off, and the answer to "Do I need coffee?" is always true โ.
Common Use-case โ
Booleans are used in control flow statements like if statements. They help your code make decisions, because even computers have commitment issues.
4. Null: The Void Awaits ๐ โ
typeofย return value: object.
null is a special value that represents the intentional absence of any object value. It's JavaScript's way of saying, "There's nothing here, and I meant it."
Common Use-case โ
Use null as a default value for variables or to signify 'no result'. It's like a placeholder for your future regrets.
5. Undefined: The Unassigned Variableโs Best Friend ๐ค โ
typeofย return value: undefined.
undefined indicates that a variable has been declared but not assigned a value. It's the JavaScript equivalent of a blank stare.
Common Use-case โ
undefined is the default return value of functions that donโt explicitly return something. It's like your code saying, "I showed up, but I didn't bring anything."
6. Symbols: Unique Snowflakes โ๏ธ โ
typeofย return value: symbol.
Symbols are unique and immutable. They are used primarily as keys for object properties to avoid property name collisions. Think of them as the VIP members of your codeโunique and irreplaceable.
Creation โ
let sym = Symbol("addressed")Common Use-case โ
Symbols are perfect for creating unique property keys. They ensure that your secret handshake remains secret.
7. BigInt: Because Regular Numbers Arenโt Big Enough ๐ โ
typeofย return value: bigint.
BigInt is a special type for integers that are too large to be represented by the Number type. It's like numbers decided to hit the gym and bulk up.
Creation โ
let big = BigInt(1234567890123456789012345678901234567890)
let big = 1234567890123456789012345678901234567890nCommon Use-case โ
BigInt is used for cryptography and high-precision calculations, because sometimes regular-sized numbers just don't cut it.
Primitive vs. Object: The Great Debate โ๏ธ โ
Mutability: Primitives are immutable, meaning their values cannot be changed. Objects, on the other hand, are mutable and can be modified. It's like the difference between a rock (primitive) and a piece of clay (object).
Performance: Primitives are generally faster because they are simpler and stored directly in the variable. Objects are more complex and can be slower to process. Think of primitives as sprinters and objects as marathon runnersโthey're built for different tasks.
