Skip to content

Latest commit

 

History

History
144 lines (133 loc) · 2.29 KB

19. 10 String Methods_Properties You Must know.md

File metadata and controls

144 lines (133 loc) · 2.29 KB

19. 10 String Methods/Properties You Must know

1. toUpperCase()

const text = "I like javascript";
const result = text.toUpperCase();

console.log(result);

Output

I LIKE JAVASCRIPT

2. toLowerCase() Method

const text = "I LIKE JAVASCRIPT";
const result = text.toLowerCase();

console.log(result);

Output

i like javascript

3. concat()

const text1 = "JavaScript";
const text2 = "Programming";

const result = text1.concat(text2);
console.log(result);

Output

JavaScriptProgramming

4. replace()

const text = "Hello. My name is Punit."
console.log(text);

const result = text.replace("Hello", "Hi");
console.log(result);

Output

Hi. My name is Punit.

5. split()

const text = "hello";
const result = text.split('');
console.log(result);

Output

["h", "e", "l", "l", "o"]

6. slice()

const text = "JavaScript";
const result = text.slice(0,4);
console.log(result);

Output

Java

7. trim()

const text = "    JavaScript   ";
const result = text.trim();
console.log(result);

Output

JavaScript

8. search()

Example 1

const text = "JavaScript is fun.";
const result = text.search("fun");
console.log(result);

Output

14

Example 2

const text = "JavaScript is fun.";
const result = text.search("funny");
console.log(result);

Output

-1

Programming Task

Create a string "I love Java". Convert the string to lowercase and check if the string consists of the substring "java". If yes, replace the substring "java" with "JavaScript".

Solution:

const text = "I love Java.";

const smallcase = text.toLowerCase();

const checkValue = smallcase.includes('java');

if(checkValue) {
    const result = smallcase.replace('java', 'JavaScript');
    console.log(result);
}

Output

i love JavaScript.

Programiz Quiz

Q. What is the output of the program?

const message = "JavaScript is fun";
const result = message.split(' ');

const value = result[0].charAt(2);
console.log(value);
  1. s
  2. v
  3. is
  4. fun

Answer: 2