DEV Community

dss99911
dss99911

Posted on • Originally published at dss99911.github.io

JavaScript Strings and Numbers - Complete Guide

Strings and numbers are fundamental data types in JavaScript. This guide covers everything you need to know about working with them effectively.

Strings

String Methods

JavaScript provides many useful string methods:

str.includes("text")      // Returns true if string contains "text" (like Java's contains)
str.lastIndexOf("text")   // Returns last index of "text"
str.substr(start, length) // Returns substring by length
str.slice(start, end)     // Returns substring (like substring, allows negative numbers)
str.length                // Returns string length
Enter fullscreen mode Exit fullscreen mode

Template Literals (ES6)

Template literals allow embedded expressions:

const name = "World";
const greeting = `Hello ${name}!`;  // "Hello World!"

const completed = true;
console.log(`Status: ${completed}`);  // "Status: true"
Enter fullscreen mode Exit fullscreen mode

String Compatibility Note

Avoid accessing strings like arrays in older browsers:

str[0];           // Does not work in IE5, IE6, IE7
str.split("")[0]; // Use this instead for compatibility
Enter fullscreen mode Exit fullscreen mode

Numbers

Parsing Numbers

parseInt("10")          // Returns 10
Number.isInteger(num)   // Check if number is an integer

// Check if value is a valid number
Number.isInteger(parseInt("1"))  // true
Enter fullscreen mode Exit fullscreen mode

Special Number Values

Infinity

var myNumber = 2;
while (myNumber != Infinity) {
    myNumber = myNumber * myNumber;  // Eventually reaches Infinity
}

var x = 2 / 0;        // Infinity
typeof Infinity;      // Returns "number"
Enter fullscreen mode Exit fullscreen mode

NaN (Not a Number)

var x = 100 / "Apple";  // NaN
isNaN(x);               // true
Enter fullscreen mode Exit fullscreen mode

Number Formatting with toFixed()

var x = 9.656;
x.toFixed(0);  // Returns "10"
x.toFixed(2);  // Returns "9.66"
Enter fullscreen mode Exit fullscreen mode

Floating Point Precision Issues

JavaScript uses floating point arithmetic which can cause precision issues:

var x = 0.1;
var y = 0.2;
var z = x + y;  // 0.30000000000000004 (not 0.3!)

// Solution: Use integer math
var z = (x * 10 + y * 10) / 10;  // 0.3
Enter fullscreen mode Exit fullscreen mode

Regular Expressions

Regular expressions provide powerful pattern matching for strings.

Replace with Regex

// Replace all occurrences (global flag)
str = "Please visit Microsoft and Microsoft!";
var result = str.replace(/Microsoft/g, "W3Schools");

// Case-insensitive replacement
var result = str.replace(/microsoft/i, "W3Schools");
Enter fullscreen mode Exit fullscreen mode

Testing Patterns

var pattern = /e/;
pattern.test("The best things in life are free!");  // Returns true
Enter fullscreen mode Exit fullscreen mode

Extracting Matches

/e/.exec("The best things in life are free!");  // Returns "e"
Enter fullscreen mode Exit fullscreen mode

Custom Prototypes

You can extend built-in types with custom methods:

// Add a contains method to String prototype
String.prototype.contains = function(text) {
    return this.indexOf(text) != -1;
};

// Usage
"Hello World".contains("World");  // true
Enter fullscreen mode Exit fullscreen mode

Note: While possible, extending built-in prototypes is generally not recommended in production code as it can cause conflicts with other libraries.

Practical Examples

Validating User Input

function isValidNumber(input) {
    const num = parseInt(input);
    return !isNaN(num) && Number.isInteger(num);
}

isValidNumber("42");    // true
isValidNumber("3.14");  // false (not an integer after parseInt)
isValidNumber("hello"); // false
Enter fullscreen mode Exit fullscreen mode

Formatting Currency

function formatCurrency(amount) {
    return "$" + amount.toFixed(2);
}

formatCurrency(19.5);   // "$19.50"
formatCurrency(100);    // "$100.00"
Enter fullscreen mode Exit fullscreen mode

Checking for Substrings

const text = "JavaScript is awesome!";

// Modern approach (ES6+)
text.includes("awesome");  // true

// Older approach (pre-ES6)
text.indexOf("awesome") !== -1;  // true
Enter fullscreen mode Exit fullscreen mode

Understanding strings and numbers is essential for any JavaScript developer. These fundamental operations form the building blocks for more complex data manipulation tasks.


Originally published at https://dss99911.github.io

Top comments (0)