Click Below to subscribe

Swap two strings without using third variable.

Given two strings S1, S2 swap it without using temp variable.

substring(start, end) - The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.

#Note - The substring() method does not change the original string.

#Implementation

let a = "Hello"; 
let b = "World"; 

a = a + b;                                // "HelloWorld"
b = a.substring(0, a.length - b.length); // substring(0, 5) -> Hello
a = a.substring(b.length);              // substring(5) -> World

console.log(a, b)
//output- World Hello

Leave Your Comment