---
title: "JavaScript -Strings"
url: https://khanwaseem.com/blog/course/javascript-strings
author: "khan Waseem"
published: 2023-10-02
updated: 2023-10-02
topics: ["course", "Javascript Tutorial"]
description: "Explore the world of JavaScript strings: Learn common methods and operations for effective text manipulation and processing in your code."
---

# JavaScript -Strings

In JavaScript, there isn’t a specific “Strings Object” like there are for some other data types, such as the Number or Boolean objects. Instead, strings are represented as primitive data types in JavaScript. However, there are several built-in methods and properties that can be used to manipulate and work with strings effectively.

Strings in JavaScript are sequences of characters enclosed within single (”), double (” “), or backticks (` `) quotes. These are some key concepts and operations related to strings in JavaScript:

**Creating Strings :**

```javascript
let greeting = "Hello, world!";Code language: JavaScript (javascript)
```

Table of Contents

[Toggle](#)

- [String Properties :](#string-properties)
- [String Methods :](#string-methods)

## String Properties :

In JavaScript, string properties are attributes associated with string values that provide additional information about those strings or enable specific operations. While strings themselves are not objects like some other data types, they still have useful properties that help developers work with them more effectively. Here are some essential string properties in JavaScript:

**1. **`**length**`** Property**:

- The `length` property returns the number of characters in a string, allowing you to determine its length.

- Example:

```javascript
let greeting = "Hello, world!";
let length = greeting.length; // length is 13Code language: JavaScript (javascript)
```

**2. `constructor` Property**:

- The `constructor` property indicates the constructor function that created the string object.

- Example:

```javascript
   let myString = "JavaScript";
   console.log(myString.constructor); // Output: [Function: String]Code language: JavaScript (javascript)
```

**3. `prototype` Property**:

- The `prototype` property is used to extend the behavior of string objects by adding properties or methods.

- Example:

```javascript
String.prototype.isUpperCase = function() {
   return this.valueOf() === this.toUpperCase();
};

let text = "Hello";
console.log(text.isUpperCase()); // Output: falseCode language: JavaScript (javascript)
```

**4. **`**constructor**`** Property**:

- The `constructor` property refers to the constructor function for string objects.

- Example:

```javascript
let sampleString = "Sample";
console.log(sampleString.constructor); // Output: [Function: String]Code language: JavaScript (javascript)
```

These string properties and methods are essential for manipulating, checking, and enhancing string values in JavaScript. Understanding and utilizing them can help you work effectively with text data in your JavaScript programs.

## String Methods :

Certainly! In JavaScript, string methods are functions that allow you to perform various operations and manipulations on strings. These methods are incredibly useful for working with text data. Here’s an overview of some common string methods in JavaScript:

**1. `charAt()` Method**:

- While not exactly a property, the `charAt()` method allows you to access a specific character at a given position within the string.

- Example:

```javascript
let phrase = "JavaScript";
let character = phrase.charAt(4); // character is 'S'Code language: JavaScript (javascript)
```

**2. `concat()` Method**:

- The `concat()` method combines two or more strings and returns a new string.

- Example:

```javascript
let firstName = "John";
let lastName = "Doe";
let fullName = firstName.concat(" ", lastName); // "John Doe"Code language: JavaScript (javascript)
```

**3. **`**toUpperCase()**`** and **`toLowerCase()`** Methods**:

- `toUpperCase()` converts all characters in a string to uppercase.

- `toLowerCase()` converts all characters in a string to lowercase.

- Examples:

```javascript
let text = "JavaScript";
let uppercaseText = text.toUpperCase(); // "JAVASCRIPT"
let lowercaseText = text.toLowerCase(); // "javascript"Code language: JavaScript (javascript)
```

**4. `indexOf()` and `lastIndexOf()` Methods**:

- `indexOf()` finds the position of the first occurrence of a specified substring in a string.

- `lastIndexOf()` finds the position of the last occurrence of a specified substring in a string.

- Examples:

```javascript
let sentence = "This is a sentence.";
let position1 = sentence.indexOf("is"); // 2
let position2 = sentence.lastIndexOf("is"); // 5Code language: JavaScript (javascript)
```

**5. `slice()` Method**:

- The `slice()` method extracts a portion of a string and returns it as a new string, based on specified start and end positions.

- Example:

```javascript
let phrase = "JavaScript";
let slicedText = phrase.slice(4, 6); // "Sc"Code language: JavaScript (javascript)
```

**6. `replace()` Method**:

- The `replace()` method replaces a specified substring or pattern with another substring and returns a new string.

- Example:

```javascript
let sentence = "JavaScript is fun!";
let newSentence = sentence.replace("JavaScript", "Programming"); // "Programming is fun!"Code language: JavaScript (javascript)
```

**7. `split()` Method**:

- The `split()` method splits a string into an array of substrings based on a specified delimiter.

- Example:

```javascript
let colors = "red,green,blue";
let colorArray = colors.split(","); // ["red", "green", "blue"]Code language: JavaScript (javascript)
```

**8. `trim()` Method**:

- The `trim()` method removes whitespace from the beginning and end of a string.

- Example:

```javascript
let userInput = "   Hello, world!   ";
let trimmedInput = userInput.trim(); // "Hello, world!"Code language: JavaScript (javascript)
```

**9. **`**valueOf()**`** Method**:

- The `valueOf()` method returns the primitive value of a string object.

- Example:

```javascript
let strObject = new String("Hello, World!");
let primitiveValue = strObject.valueOf();
console.log(primitiveValue); // Output: "Hello, World!"Code language: JavaScript (javascript)
```

These string methods are just a few examples of the many functions available in JavaScript for working with text. They provide powerful tools for manipulating and processing strings, making them an essential part of JavaScript programming.

### Related posts:

[Regular Expressions and RegExp Object](https://blog.khanwaseem.com/regular-expressions-and-regexp-object/)
[JavaScript – Arrays](https://blog.khanwaseem.com/javascript-arrays/)
[CSS – Text](https://blog.khanwaseem.com/css-text/)
[JavaScript – Events](https://blog.khanwaseem.com/javascript-events/)

Powered by [YARPP](https://yarpp.com).
