In JavaScript, null
is a primitive value that represents the intentional absence of any object value. It is one of JavaScript’s primitive data types and is often used to indicate that a variable should be empty or that a value is unknown or not yet assigned.
Key Points about null
:
- Type of
null
:
typeof null; // "object"
- This is a well-known bug in JavaScript.
null
is not actually an object, but due to legacy reasons,typeof null
returns"object"
.
- Use Cases:
-
To explicitly indicate “no value” or “no object”:
let user = null; // No user currently
-
Often used in comparisons to check for the absence of an object:
if (user === null) { // handle null case }
- Checking for
null
: You can check fornull
using:
if (value === null) {
// do something
}
- Falsy Value:
null
is a falsy value, which means it evaluates tofalse
in a Boolean context:
if (!null) {
console.log("null is falsy");
}
In JavaScript, undefined
is a primitive value that means a variable has been declared but has not yet been assigned a value. It’s one of the core primitive data types in JavaScript.
🔹 What is undefined
?
When JavaScript declares a variable but doesn’t initialize it, the value is automatically set to undefined
:
let a;
console.log(a); // undefined
🔹 When does undefined
occur?
- Uninitialized variables:
let x;
console.log(x); // undefined
- Function with no return statement:
function doSomething() {}
console.log(doSomething()); // undefined
- Missing function parameters:
function greet(name) {
console.log(name);
}
greet(); // undefined
🔹 Type of undefined
typeof undefined; // "undefined"
🧩 What Are Template Literals in JavaScript?
Template literals (also called template strings) are a modern way to work with strings in JavaScript, introduced in ES6 (ECMAScript 2015). They make it easier to embed variables, expressions, and multi-line strings without complex concatenation.
✅ Syntax
Template literals use backticks (`
) instead of single ('
) or double ("
) quotes.
const name = "Alice";
const message = `Hello, ${name}!`;
console.log(message); // Hello, Alice!
🔹 Key Features of Template Literals
1. String Interpolation
Embed variables and expressions using ${...}
:
const a = 5;
const b = 10;
console.log(`The sum of ${a} and ${b} is ${a + b}`); // The sum of 5 and 10 is 15
2. Multi-line Strings
No need for \n
or string concatenation:
const multiLine = `This is line one.
This is line two.`;
console.log(multiLine);
3. Expression Evaluation
You can include any valid JavaScript expression:
const x = 10;
console.log(`Ten times two is ${x * 2}`); // Ten times two is 20
4. Function Calls Inside Template Literals
function greet(name) {
return `Hello, ${name.toUpperCase()}!`;
}
console.log(greet("bob")); // Hello, BOB!
5. Tagged Template Literals (Advanced)
A tagged template is a function that processes a template literal:
function tag(strings, ...values) {
console.log(strings); // [ 'Hello ', ', you are ', ' years old.' ]
console.log(values); // [ 'Alice', 30 ]
return `${strings[0]}${values[0].toUpperCase()}${strings[1]}${values[1]}${strings[2]}`;
}
const result = tag`Hello ${'Alice'}, you are ${30} years old.`;
console.log(result); // Hello ALICE, you are 30 years old.
Tidak ada komentar:
Posting Komentar