JavaScript Recursion: Solving Problems in a Neat Way

In JavaScript, recursion refers to the ability of a function to call itself. This powerful concept allows us to solve problems in a clean and efficient way. To implement recursion, you need to use a named function expression. Let’s dive into its basics. Consider the simple example of calculating the factorial of a number. The factorial of a number is obtained by multiplying it with all the positive integers less than itself....

JavaScript Reference: String

Learn about the various properties and methods of the JavaScript String object. The String object has a static method called String.fromCharCode() that allows you to create a string representation from a sequence of Unicode characters. You can use this method to build a simple string using ASCII codes. Example: String.fromCodePoint(70, 108, 97, 118, 105, 111) //'Flavio' You can also use octal or hexadecimal numbers: Example: String.fromCodePoint(0x46, 0154, parseInt(141, 8), 118, 105, 111) //'Flavio' All the other methods described in this article are instance methods, meaning they are run on a string type....

JavaScript Return Values: Understanding the Basics

In JavaScript, every function has a return value, even if it is not explicitly specified. By default, if no return statement is provided, the return value is undefined. However, you can use the return keyword to specify the value that should be returned. For example: const doSomething = () => { return 'test'; } const result = doSomething(); // result === 'test' In this example, the function doSomething returns the string 'test'....

JavaScript Symbols: Explained and Utilized

Symbols are a unique and powerful data type in JavaScript, introduced in ECMAScript 2015. Unlike other primitive data types like strings, numbers, booleans, null, and undefined, symbols have a distinct characteristic - their values are kept private and meant for internal use only. Once created, symbols are immutable and their values remain hidden, with only the symbol reference accessible. To create a symbol, you can use the Symbol() global factory function....

JavaScript Type Conversions: A Guide

Learn the fundamentals of JavaScript type conversions Even though JavaScript is a loosely typed language, there may arise situations where you need to convert a value from one type to another. In JavaScript, we have primitive types like numbers, strings, booleans, and symbols, along with the object type. However, converting from or to null and undefined is not necessary. Here are some common conversion scenarios and techniques to achieve them:...

JavaScript Typecasting: Converting Data Types

Learn how to convert data types in JavaScript In JavaScript, although it is a loosely typed language, there may be instances where you need to convert values from one type to another. JavaScript has these primitive types: Number String Boolean Symbol and the object type: Object (plus null and undefined, but there’s no point in casting from/to them) Let’s explore different techniques for converting from one type to another. We’ll cover the most common scenarios....

JavaScript typeof Operator

In JavaScript, every value has an assigned type. To determine the type of a variable, we can use the typeof operator, which returns a string representing the variable’s type. Here are a few examples of using the typeof operator: typeof 1; //'number' typeof '1'; //'string' typeof {name: 'Flavio'}; //'object' typeof [1, 2, 3]; //'object' typeof true; //'boolean' typeof undefined; //'undefined' typeof (() => {}); //'function' typeof Symbol(); //'symbol' It’s interesting to note that JavaScript doesn’t have a specific “function” type....

JavaScript vs Python: Which Language to Choose for Beginners?

People often ask me which programming language they should start with: JavaScript or Python. As a senior developer and author of books on both JavaScript and Python, I can tell you that both languages are great options for beginners and extremely popular in the industry. However, there are certain differences between them that can help you make an informed decision. JavaScript, being the language of the web, has a significant advantage in frontend development....

JavaScript: How to Get a Substring Until a Specific Character

In JavaScript, there are several ways to extract a substring from a string until a specific character is encountered. This can be useful when you want to retrieve the first part of a string before a certain delimiter, such as a hyphen ("-"). One simple approach is to use the split() method, which splits a string into an array of substrings based on a specified delimiter. By specifying the hyphen as the delimiter, we can extract the desired substring by accessing the first element of the resulting array....

JavaScript: Passing Values by Reference or by Value

In JavaScript, the way values are passed depends on whether they are primitive types or objects. Primitive types, which include numbers, strings, booleans, null, undefined, and symbols, are passed by value. When a primitive value is passed to a function, a copy of that value is made, and any modifications made to the copy will not affect the original value. For example: const increment = num => { num = num + 1; } const num = 2; increment(num); console....