ALL
The Strange Behavior of the void Type in TypeScript
PrefaceIn TypeScript (TS), there is a type called void. It represents "nothing" but it's important to note that it's not the same as "nothing" in JavaScript(JS).Typically, void is used to declare the return type of functions. Although you can declare a variable as void, we generally don't do this because it serves no meaningful purpose. Let's explore why in the examples below.The void TypeDeclaring a Variable as voidlet name: void; // Declare a variable `name` with type `void`.name = 'nanjiu'; // Error: Cannot assign type 'string' to type 'void'.name = 18; // Error: Cannot assign type 'number'...
5,562 1 JAVASCRIPT TYPE TYPESCRIPT VOID
Event Loop Mechanism in JavaScript
IntroductionThe Event Loop MechanismThe event loop mechanism is the core of how JavaScript handles asynchronous operations. It ensures that the execution order of the code aligns with the expected sequence.JavaScript's Single ThreadJavaScript is known for being a single-threaded language, meaning it executes one task at a time. This can lead to a significant issue: if a thread is blocked, the entire program becomes unresponsive. To address this, JavaScript introduced the event loop mechanism. This mechanism allows JavaScript to handle asynchronous operations while executing tasks, thus improvi...
2,675 0 EVENT LOOP EVENT MECHANISM JAVASCRIPT
The Pitfall of WebSocket Disconnections Caused by Browser Power-Saving Mechanisms
PrefaceRecently, while using WebSocket (WS) connections, we encountered frequent disconnection issues, occurring hundreds of times per day for a single user. Although using the auto-reconnect feature of socket.io allowed us to quickly restore connections after disconnections, it did not guarantee that every reconnection would successfully receive WS messages. Therefore, we conducted several investigations and tests.Ultimately, we identified the root cause of the issue: the browser's power-saving mechanism, which inadvertently became the culprit behind the problem.Overview of Browser Power-Savi...
The four key figures behind the success of JavaScript - Douglas Crockford
JavaScript's success can be attributed to at least four key figures:Brendan Eich, the creator of JavaScriptDouglas Crockford, the creator of JSLint and JSONJohn Resig, the creator of jQueryRyan Dahl, the creator of Node.js.We are already very familiar with Brendan Eich and the invention process of JavaScript, so let's start with Douglas Crockford, the second in command of JavaScript.AllianceIn the 1990s, Microsoft's dominance overshadowed the whole world.At this time, two challengers emerged, one was the IT giant Sun, and the other was the IT upstart Netscape.Sun believed that Java programs co...
4,404 0 JAVASCRIPT HISTORY DOUGLAS CROCKFORD
JavaScript's New Features: Exploring the Latest Additions to the Language
As the web continues to evolve, so too does the JavaScript ecosystem. With the release of ECMAScript 2023 (ES2023), JavaScript developers can now leverage a range of new features that promise to improve code quality, readability, and maintainability. In this blog post, we'll dive into some of ES2023's most exciting additions and provide practical examples of how they can be used in your projects.1. Private Fields and Methods in ClassesOne of the most anticipated features in ES2023 is the introduction of private fields and methods in classes. These enable developers to encapsulate internal clas...
1,472 0 JAVASCRIPT ES2023
Understanding JavaScript closure and where it can be used
ClosureThe official definition of closure is: an expression (usually a function) that has many variables and is bound to an environment that includes those variables.In JavaScript, closure refers to the ability of a function to access the lexical scope in which it was defined, even after the function has been executed and left that scope. This ability is due to the fact that when a function is created, it generates a closure that includes a reference to the definition environment of the current function, allowing the function to continue to access those variables within that environment.Below ...
1,535 0 CLOSURE JAVASCRIPT USAGE
Let's talk about JavaScript deep clone
In JavaScript, deep clone means creating a brand new object that includes all nested objects, with all properties being completely independent copies. This is different from shallow copying, which only copies the first-level properties, with nested objects being referenced rather than copied.There are multiple ways to perform deep copying in JavaScript, but the best one to use depends on the specific use case.Can use JSON.parse & JSON.stringify? ❌JSON.parse(JSON.stringify(obj)) is a depth cloning method that uses JSON serialization to create an object. It works for objects that do not co...
3,783 0 JAVASCRIPT DEEP CLONE
Mastering Async/Await in JavaScript: A Comprehensive Guide with Examples
As a veteran JavaScript developer, I have seen the evolution of JavaScript's asynchronous programming landscape. From callbacks to promises and now async/await, JavaScript has come a long way in making it easier to handle asynchronous operations in a clean and readable manner.In this article, we will delve into the world of async/await and explore why it has become a popular choice for handling asynchronous operations in JavaScript. We will cover the basics of async/await, its syntax, and how it differs from traditional promises. Additionally, we will go through several examples of using async...
1,922 0 JAVASCRIPT COMPARISON PROMISE ASYNC AWAIT