Understand NaN (Not a Number) in JavaScript
number.console.log(typeof NaN); // "number"website·Indexed 30 days ago
https://katechou37.github.ioA technical blog by a front-end engineer and designer specializing in React and TypeScript. The site features tutorials and notes on JavaScript, including variable declaration, string length (.length), NaN, and synchronous vs asynchronous execution, as well as guides on Hexo installation.
number.console.log(typeof NaN); // "number"async and await syntax.JavaScript is a single-threaded language, meaning it can only execute one task at a time.
setTimeout).JavaScript is a single-threaded language, meaning it can only execute one task at a time. Tasks are processed sequentially; the next line of code will not run until the current line has finished.
Synchronous Execution (Blocking): Tasks are performed in a strict sequence (like a queue). If a task takes a long time or gets stuck, it blocks all subsequent code from executing.
Asynchronous Execution (Non-blocking): JavaScript initiates a task and moves on to the next one without waiting for the first to complete. Once the asynchronous task finishes, the program is notified or executes a callback. This is essential for operations that take an unpredictable amount of time, such as:
setTimeout).JavaScript is a single-threaded language, meaning it can only execute one task at a time.
alert), the entire program stops until that task completes.Common asynchronous scenarios include:
setTimeout).JavaScript provides two primary ways to declare variables:
let: Used for variables that need to be reassigned later.const: Used for constants that should not be reassigned.Naming Rules:
name and Name are different variables).let, if, for).let apple = 4; // Can be reassigned
const pi = 3.14; // Cannot be reassigned
// Correct naming
let apple1 = 3;The async and await keywords provide a way to write asynchronous code that looks and behaves like synchronous code.
async: Marks a function as asynchronous, ensuring it returns a Promise.await: Pauses the execution of the async function until the Promise is resolved. Crucially, it does not block the entire browser; it only pauses that specific function while the engine handles other tasks.async function fetchData() {
const response = await fetch('/api/users');
const data = await response.json();
console.log(data);
}+ operator. If any operand is a string, JavaScript automatically converts the other operand to a string. For better readability, use template literals with backticks (`) and ${} placeholders.let friendName = "Tom";
let clause = "How are you?";
// Using + operator
let total = friendName + " " + clause;
// Using template literal (Recommended)
let totalLiteral = `${friendName} ${clause}`;Use let for variables that need to be reassigned and const for constants that should not change.
Naming Rules:
name and Name are different variables).let, if, for).let apple = 4; // Can be reassigned
const pi = 3.14; // Cannot be reassigned
// Correct naming
let apple1 = 3;== or ===) will always return false. To correctly identify if a value is NaN, use Number.isNaN().// Equality check fails
console.log(NaN === NaN); // false
// Recommended check
Number.isNaN(NaN); // truelet to declare variables that can be reassigned. Use const to declare constants that cannot be reassigned after the initial value is set.let apple = 4; // Variable: can be reassigned
const pi = 3.14; // Constant: cannot be reassigned