Code Monkey home page Code Monkey logo

javascript's Introduction

JavaScript

A Code Repo for JavaScript

A Quick Guide to String Methods in JavaScript

Strings are a fundamental data type in JavaScript, representing sequences of characters. Manipulating strings is a common task in web development, and JavaScript provides a plethora of built-in methods to help with this. In this short guide, we'll introduce you to some of the most commonly used string methods.

1. length

The length property is not a method, but it's essential for every JavaScript developer to know. It returns the number of characters in a string.

const greeting = "Hello, World!";
const length = greeting.length; // length will be 13

2. toUpperCase() and toLowerCase()

These methods allow you to change the case of the characters in a string.

const message = "Hello, World!";
const upperCaseMessage = message.toUpperCase(); // "HELLO, WORLD!"
const lowerCaseMessage = message.toLowerCase(); // "hello, world!"

3. charAt(index)

The charAt() method returns the character at a specified index.

const str = "Hello";
const char = str.charAt(1); // "e"

4. concat(str1, str2, ...)

This method concatenates two or more strings and returns a new string.

const firstName = "John";
const lastName = "Doe";
const fullName = firstName.concat(" ", lastName); // "John Doe"

5. slice(start, end)

slice() extracts a portion of a string and returns it as a new string. The start index is inclusive, and the end index is exclusive.

const str = "Hello, World!";
const sliced = str.slice(0, 5); // "Hello"

6. indexOf(substring, start) and lastIndexOf(substring, start)

indexOf() returns the index of the first occurrence of a specified substring. lastIndexOf() does the same, but starts searching from the end of the string.

const sentence = "To be or not to be, that is the question.";
const index = sentence.indexOf("be"); // 3
const lastIndex = sentence.lastIndexOf("be"); // 16

7. replace(searchValue, replaceValue)

replace() searches a string for a specified value and replaces it with another value.

const str = "Hello, World!";
const newStr = str.replace("World", "Universe"); // "Hello, Universe!"

8. trim()

trim() removes whitespace from both ends of a string.

const str = "   Hello, World!   ";
const trimmed = str.trim(); // "Hello, World!"

Array Methods

In JavaScript, there are several array methods that manipulate the original array. These methods directly modify the array they are called on. Here are some commonly used array methods that perform in-place modifications:

push(): Adds one or more elements to the end of an array and returns the new length of the array.

Example:

Copy code
const myArray = [1, 2, 3];
myArray.push(4); // myArray is now [1, 2, 3, 4]

pop(): Removes the last element from an array and returns that element.

Example:

Copy code
const myArray = [1, 2, 3];
const poppedElement = myArray.pop(); // myArray is now [1, 2], poppedElement is 3

shift(): Removes the first element from an array and returns that element.

Example:

Copy code
const myArray = [1, 2, 3];
const shiftedElement = myArray.shift(); // myArray is now [2, 3], shiftedElement is 1

unshift(): Adds one or more elements to the beginning of an array and returns the new length of the array.

Example:

Copy code
const myArray = [2, 3];
myArray.unshift(1); // myArray is now [1, 2, 3]

splice(): Changes the contents of an array by removing, replacing, or adding elements. It can be used to add, remove, or replace elements at any position.

Example:

Copy code
const myArray = [1, 2, 3, 4];
myArray.splice(1, 2); // myArray is now [1, 4]

reverse(): Reverses the order of the elements in an array.

Example:

Copy code
const myArray = [1, 2, 3, 4];
myArray.reverse(); // myArray is now [4, 3, 2, 1]

sort(): Sorts the elements of an array in place and returns the sorted array.

Example:

Copy code
const myArray = [3, 1, 4, 1, 5, 9, 2, 6];
myArray.sort(); // myArray is now [1, 1, 2, 3, 4, 5, 6, 9]

Remember, when using these methods, you're directly modifying the original array. If you want to avoid modifying the original array, you can create a copy of it using methods like slice() or spread syntax ([...myArray]) and then perform the desired operations on the copy.

Node doesn't have DOM API

Screenshot (90)

Screenshot (92)

Screenshot (97)

Screenshot (100)

Screenshot (101)

javascript's People

Contributors

divyagupta167 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.