sabe.io sabe.io

How to Convert a String to a Number in JavaScript

Numbers can be used in JavaScript in both strings and number form. In some cases, we'll need to able to convert a string to a number so that we can use it in mathematical operations. Thankfully, there are several built-in ways to do this in JavaScript. In this post, we'll go over the two best ways to convert a string to a number in JavaScript. Using the parseInt() method The most straight-forward way to convert a string to a number is...

sabe.io sabe.io

How to Convert a List to Comma-Separated String in Java

Lists in Java are a collection of items that can be held in a single variable. If this list is holding strings inside, you have the option to convert it to a comma-separated string. In this post, we'll learn the two best ways to turn a string list into a comma-separated string in Java. Using the join() method The most straight-forward way to convert a string list to a comma-separated string is to use the join() method that exists on...

sabe.io sabe.io

How to get the Width and Height of Browser's Viewport in JavaScript

The browser is the program that takes your HTML, CSS, and JavaScript, and renders it on the screen. With that being said, the actual size of the canvas is going to be different depending on the screen size, browser, device type, and other factors. For example, mobile phones will have a smaller viewport than desktop computers. Because of this, as front-end developers, we need a way to get the size of the viewport of the browser so we...

sabe.io sabe.io

How to Print an Array in PHP

When you're working with arrays in PHP, one of the most common things you'll need to do is print them out to the screen. This is useful for when you are trying to debug your code or simply to check the value of the elements in the array. In this post, we'll learn the best ways to print out an array in PHP. Using a foreach loop The most straightforward way to print out an array is to use a foreach loop. This involves simply iterating...

sabe.io sabe.io

How to get the Max Value of an Integer in Java

In Java, integers are the primitive data type that is used to store numbers that don't have a fractional component. Like in any programming language, integers are either positive or negative, and they do have an upper limit. In this post, we'll learn how you can get the maximum value of an integer in Java. Getting the maximum value of an integer Let's look at an example of some integers and print them to the console. public class...

sabe.io sabe.io

How to add Days to a Date in JavaScript

Working with dates in JavaScript has historically been a challenge. One of most commonly needed operations is how to add a number of days to a date to get a new date. In this post, we'll look at the easiest way you can add a number of days to another date Adding days to a date Let's first start by creating a date object. const date = new Date(); To increase the days on a Date, we can take advantage of the Date object's getDate() and...

sabe.io sabe.io

Get Index of Object in Array by Property in JavaScript

When you have an array of objects, finding if an object exists in it is trickier than if the array was of primitives, like strings or numbers. This is because objects can take many different forms and have different data values in them. In this post, we'll learn how you find the index of an object in an array by property in JavaScript. Finding the Index of an Object in an Array by Property First let's start off with an example...

sabe.io sabe.io

How to Check a Variable Type in Python

In Python, you can use variables to store data, pass data between functions, and manipulate data. This data is held in memory for the duration of the program and is lost when the program ends. Variables can be of a different type because data comes in many different forms. In this post, we'll learn about how you can check the type of a variable in Python. Variable Types in Python Before we start, let's get familiar with the different...

sabe.io sabe.io

How to calculate Exponents in Python

Exponentiation is the operation in which a number is multiplied by itself a certain number of times. Just like in the real-world, using exponents is useful in programming languages whenever you want to repeat multiplication. In this post, we'll look at how to calculate the exponent of a number in Python. Using the ** operator The easiest way to calculate the exponent of a number is to use the ** operator. This operator uses the...

sabe.io sabe.io

How to get the Class name of an Object in JavaScript

JavaScript supports object-oriented programming, which means that you can create classes and objects, and use them to store data. One useful thing to be able to know from an object is the name of the class it was created from. In this post, we'll learn how you can get the class name of an object in JavaScript. Getting the class name of an object First let's start with a class and an object created from it. class Shape {} const...