Home » Programming » Javascript

How to Create and Use Enums in JavaScript (Not TypeScript)

JavaScript Enum

This article will show you how to create and consume Enums in JavaScript. Unfortunately, javaScript does not include native support for Enums, but you can add comparable functionality yourself. Note that TypeScript, a language that builds on JavaScript, does include support for Enums natively. So we’re only looking at plain JavaScript here – not TypeScript. What is an Enum? An Enum (Enumerated Type) is a data structure containing multiple values. Each value is assigned to an identifier – and can be accessed by that identifier. Enums contain pre-defined constants … Read more

Home » Programming » Javascript

Generate Random Numbers and Strings in JavaScript [Examples]

Javascript Random String Number

This guide will show you how to quickly and easily generate random numbers and strings in the JavaScript programming language. Generating random values is a common and important task when programming almost any kind of application of moderate complexity. Random strings and numbers are used to generate unique identifiers (e.g., for the short URLs in URL shortening services), for identifying unique records in a database, and (most importantly) are frequently used to determine the behavior of gameplay elements in video games (for example, simulating a … Read more

Home » Programming » Javascript

JavaScript String split() Method, With Examples

JavaScript Split Method

Want to split a string up into several smaller strings using JavaScript? This is the article for you. The JavaScript string.split() method will split up a string and return an array of strings. The string will be split at the position noted by a specified character. Here’s how to use it. JavaScript string.split() Syntax A method is a function or procedure available to run from an object or variable which will be run using the value from that variable. The split() method is available on any string typed variable. Here’s the … Read more

Home » Programming » Javascript

How to Use the Ternary Operator in JavaScript, With Examples

JavaScript Ternary Operator

This short article will explain what the ternary operator is in JavaScript and how to use it. The ternary operator is a short-hand if statement for quickly executing code based on whether a condition is met. It simplifies your code and reduces visual clutter. Here’s how to use it. JavaScript Ternary Operator Syntax The syntax for using the ternary operator is as follows: CONDITION ? TRUE_EXPRESSION : FALSE_EXPRESSION Note that: CONDITION should be a value or expression which can be evaluated as truthy or not truthy TRUE_EXPRESSION is the expression that will be … Read more

Home » Programming » Javascript

What is ‘undefined’ in JavaScript?

JavaScript Undefined

This short article will explain what ‘undefined’ means in JavaScript – as both a type and a variable value. Creating a Variable with an undefined value To create a variable with an undefined value, it simply needs to be declared with no assigned value, for example: var myVariable; console.log(myVariable); If the above code is executed, undefined is logged as the value of myVariable as no value was assigned. undefined is a Type of Variable undefined is one of the primitive variable types in JavaScript. A variable type describes what a variable can be used for (for … Read more

Home » Programming » Javascript

Beginners Guide: Create and Use JavaScript ES6 Modules

JavaScript Modules

This short tutorial aims to get you started creating and using JavaScript modules and provides some simple examples. What is ES6? JavaScript’s official name is actually ECMAScript, and version 6 introduced module functionality to create and consume modules, so you might see JavaScript Modules referred to as ES6 or ES2015 Modules. What is a JavaScript Module? As you get more adventurous with your JavaScript programming projects and your code becomes more complex, it might start to become harder to manage. One way of mitigating this is splitting … Read more

Home » Programming » Javascript

Check Type of Variable in JavaScript with typeof [Examples]

JavaScript typeof Operator

This article will explain JavaScript variable types and how to find the type of a variable using the typeof operator. The typeof operator is quite similar to the instanceof operator – but they do not function the same way.  instanceof returns TRUE or FALSE when checking if a variable is of a certain type, whereas typeof returns the name of the type. What are Types? The type of a variable determines what it can or can’t do. It determines what value the variable may take and … Read more

Home » Programming » Javascript

How to use the JavaScript trim Method, with Examples

JavaScript trim

This easy tutorial will show you how to use the JavaScript string trim() method and give some example usage. The trim()* method is available to any string type variable in JavaScript. It removes any white space (spaces, tabs, newlines) found at the beginning and the end of the string. Why is this useful? Sometimes you wind up with a bunch of un-needed white space padding your strings – often from the end-user hammering the space key when a space isn’t required. Unnecessary white space can also occur after splitting … Read more

Home » Programming » Javascript

How to Get the Last Item in a JavaScript Array [Quick Tip]

JavaScript - Get last item in array

This quick article will show you how to get the last item in an array in the JavaScript programming language. Arrays and Array Indexes in JavaScript Arrays are an ordered list of items. Each item in the array has a numerical index that defines its permission in the array. The first index is index 0 (indexes start counting at 0, not 1!), so the last index is the array’s length; subtract 1. Getting the Last Item in an Array So, to get the last item in an array, we just … Read more

Home » Programming » Javascript

How to Filter Arrays in JavaScript, With Examples

Javascript: filter arrays

We’ve covered arrays in JavaScript pretty extensively on LinuxScrew. This article will show you how to use the filter method – with easy-to-follow examples. JavaScript Arrays Arrays are a type of variable that holds a list of other values or variables. They’re one of the fundamentals of computer programming. These lists contain items at positions (called indexes). These items can be anything – numbers, strings, complex objects – whatever you want to store. Arrays are super useful. You might use them to store the rows in a … Read more