Sometimes, we want to compare JavaScript strings and get end difference.
In this article, we’ll look at how to compare JavaScript strings and get end difference.
How to compare JavaScript strings and get end difference?
To compare JavaScript strings and get end difference, we can use the JavaScript string’s split
and join
methods.
For instance, we write:
const a = "The quick brown fox"
const b = "The quick brown fox jumps over the lazy dog."
const diff = (diffMe, diffBy) => diffMe.split(diffBy).join('')
const c = diff(b, a)
console.log(c)
We create the diff
function by calling split
with diffBy
and then calling join
to join back the substring array returned by split
.
Then we call diff
with b
and a
to split b
with a
as the separator.
As a result, we get c
, which is ' jumps over the lazy dog.'
.
Conclusion
To compare JavaScript strings and get end difference, we can use the JavaScript string’s split
and join
methods.