Javascript Useful Code Snippets Part - 1

Javascript Useful Code Snippets Part - 1

Certainly! Below are code snippets with explanations for the mentioned JavaScript tasks:

1. JavaScript String Length

// Code snippet for getting the length of a string in JavaScript
const myString = "Hello, World!";
const lengthOfString = myString.length;

console.log(`Length of the string: ${lengthOfString}`);

Explanation: In this code, we use the length property of a JavaScript string to determine the number of characters in the string. The result is stored in the variable lengthOfString, and it is then logged to the console.

2. JavaScript String Contains

// Code snippet for checking if a string contains another string in JavaScript
const mainString = "Hello, World!";
const subString = "World";

const containsSubstring = mainString.includes(subString);

console.log(`Does the main string contain the substring? ${containsSubstring}`);

Explanation: The includes method is used to check if a substring is present in the main string. It returns a boolean value (true if the substring is found, false otherwise). In this example, it checks if the mainString contains the subString.

3. JavaScript Convert String to Number

// Code snippet for converting a string to a number in JavaScript
const numericString = "42";
const numericValue = Number(numericString);

console.log(`Converted numeric value: ${numericValue}`);

Explanation: The Number function in JavaScript is used to convert a string to a number. In this example, the string "42" is converted to the numeric value 42, and the result is logged to the console.

4. JavaScript Append to Array

// Code snippet for appending an element to an array in JavaScript
const myArray = [1, 2, 3];
const newElement = 4;

myArray.push(newElement);

console.log(`Updated array: ${myArray}`);

Explanation: The push method is used to add a new element to the end of an array in JavaScript. In this example, the value 4 is appended to the myArray, and the updated array is logged to the console.

5. JavaScript Sleep

// Code snippet for creating a sleep function in JavaScript
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

// Example usage
console.log("Start");
await sleep(2000); // Sleep for 2000 milliseconds (2 seconds)
console.log("End");

Explanation: The sleep function is created using setTimeout and a Promise. It allows you to pause the execution of code for a specified duration. In this example, “Start” is logged, then the code sleeps for 2 seconds using await sleep(2000), and finally, “End” is logged after the sleep period. Note that the sleep function should be used within an async context.

"appId" and "appKey" is required, otherwise comment won't work.
Get your APP-ID and APP-Key

Related Posts

Redux State Management Simplified

Redux State Management Simplified

Redux is a state management library for JavaScript applications, particularly useful in managing the state of large and complex applications. It provides a predictable and centralized way to manage the application’s state and makes it easier to understand, debug, and maintain the state transitions.

Read More
React JS Roadmap

React JS Roadmap

React.js is an open-source JavaScript toolkit for creating user interfaces (UIs) or user interface components for online applications. Facebook and a network of individual developers and corporations created and manage it.

Read More
React Js Introduction

React Js Introduction

In the world of web development, creating interactive and responsive user interfaces is paramount. This is where React.js comes into play.

Read More
Top 9 NPM Packages for Supercharging Your React.js Projects

Top 9 NPM Packages for Supercharging Your React.js Projects

Today, I’d like to share with you a selection of the top 9 NPM (Node Package Manager) packages that are commonly needed when working on React.

Read More
Javascript Snake Game

Javascript Snake Game

Creating a JavaScript Snake Game involves using HTML for the structure, CSS for styling, and JavaScript for the game logic. Here’s a simple example of a Snake Game:

Read More
Understanding Logical Operators in JavaScript

Understanding Logical Operators in JavaScript

When it comes to building robust and dynamic web applications, mastering the art of conditional logic is essential. JavaScript, as one of the primary languages of web development, offers a suite of logical operators that enable developers to create sophisticated decision-making processes.

Read More