
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Adding a function for swapping cases to the prototype object of strings - JavaScript
In JavaScript, we can write our own custom functions and assign them to the existing standard data types (it is quite similar to writing library methods but in this case the data types are primitive and not user defined. We are required to write a JavaScript String function by the name, let’s say swapCase().
This function will return a new string with all uppercase characters swapped for lower case characters, and vice versa. Any non-alphabetic characters should be kept as they are.
Example
Following is the code −
const str = 'ThIS iS A CraZY StRInG'; String.prototype.swapCase = function(){ let res = ''; for(let i = 0; i < this.length; i++){ if(this[i].toLowerCase() === this[i].toUpperCase()){ res += this[i]; continue; }; if(this[i].toLowerCase() === this[i]){ res += this[i].toUpperCase(); continue; }; res += this[i].toLowerCase(); }; return res; }; console.log(str.swapCase());
Output
Following is the output in the console −
tHis Is a cRAzy sTriNg
- Related Articles
- JavaScript function that lives on the prototype object of the Array class
- How to access a JavaScript object using its own prototype?
- How to create an object with prototype in JavaScript?
- Adding binary strings together JavaScript
- Accessing variables in a constructor function using a prototype method with JavaScript?
- Adding a unique id for each entry in JSON object in JavaScript
- Importance of function prototype in C
- What is the purpose of a function prototype in C/C++?
- JavaScript Array prototype Constructor
- Function to find out palindrome strings JavaScript
- Swapping adjacent words of a String in JavaScript
- How does JavaScript .prototype work?
- What is function prototype in C language
- Program to equal two strings of same length by swapping characters in Python
- How to return an object from a JavaScript function?

Advertisements