Strings are collection of characters stored within quotes (double or single). Strings can have lower case, upper case, special characters or numbers within quotes. Strings can store data, like name, email id, age etc. Default datatype for input, select, textarea value is always a string.
In JavaScript, Strings are
declared inside double quotes "" or single quotes ''. Strings started
with double quotes should be closed by double quote and same strings started
with single quote should be closed by single quote. Both var x="hello" and var x='hello' are same in
javascript.
Backslash, i.e. \ is used inside strings to ignore
character. For exp var
x="abc\"pqr";
Example
Template Literals
ES6 introduced Template Literals or Template Strings in JavaScript by using back-tick or grave characters. To insert a placeholder variable in template literal, use ${expression} inside.
String
Methods and Properties
Primitive values, like "John Doe", cannot have properties or methods (because they are not objects). Properties are information about that string. Properties are checked using Dot notation (.) or brackets [ ] with property name in quotes. See example
String.length property shows the number of characters in string.
String length property is immutable, means if length of string is 8 and we assign string.length to 10, it will still return 8. See example
Empty String: An empty string is the string with length equals to zero.
String Methods
JavaScript Methods are
build in functions used to perform an action to strings. All methods are called
by method name and then parenthesis. Parameters are passed inside parenthesis
if required. For exp, x.age is
property, but x.getAge() is a method.
indexOf: This method returns index of first
matched substring. The return value of indexOf() methods is
always a number.
If substring is not found, it will return -1.
var x = 'Beta Labs';
lastIndexOf: This method returns index of last matched
substring. The return value of lastIndexOf() method will also
be number.
If substring is not found, it will return -1.
concat: concat() method is used to concatenate or merge two strings into one. The second string should be passed as argument.
+ operator can also concat two strings in javascript.
charAt: charAt() method return character at given index in argument.
charCodeAt: charCodeAt() method return ASCII code of character at given index in argument.
toUpperCase: toUpperCase() method convert all lowercase characters to uppercase.
toUpperCase method
only return string in uppercase, but value of x will remain same, .ie.
lowercase
toLowerCase: toLowerCase() method convert all uppercase characters to lowercase.
substr : substr() method return substrings from index (first argument ) to given no of characters (second argument). First argument is always index and second is no of characters. If second argument is missing, it will return substrings after first index. See examples
For IE8 and below, use substring
search: search() method search for a matched pattern between string and regular expression and return index of matched pattern.
To know more about regular expressions, click here. JavaScript Regular Expressions
trim: trim() method trim extra whitespace from beginning and end.
replace: replace() method is used to replace a single or multiple characters with a new characters. The first argument is replaced character, and second is replacing one.
var x = 'beta labs';
split: split() method splits a string to array
var x = 'beta labs';
JavaScript Numbers
can be integers (example 3) or whole numbers (example 3.14). JavaScript Numbers can
perform addition, subtraction, multiplication, division and Modulus. Thus all
arithmetic operations can be performed using JavaScript numbers. JavaScript
Supports all numbers systems, including Binary,
Octal, Decimal, and Hexadecimal numbers. A number starting with 0 is by-default
octal in JavaScript. For example, 010 is 8 in JavaScript.
- For fixed numbers, const is also used.
- typeof operator can check datatype of numbers.
Binary, Octal, Decimal and Hexadecimal Numbers
Here is a comparison of Binary
numbers, Octal numbers, Decimal
numbers and Hexadecimal numbers.
Number |
Binary |
Octal |
Decimal |
Hexadecimal |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
2 |
10 |
2 |
2 |
2 |
8 |
1000 |
10 |
8 |
8 |
10 |
1010 |
12 |
10 |
a |
15 |
1111 |
17 |
15 |
f |
16 |
10000 |
20 |
16 |
10 |
Numbers Method: Numbers Methods are used to convert a number to string, exponential, precision and fixed. Here are number methods with examples.
JavaScript Number Methods |
||
Method |
use |
Example |
toString() |
convert
number to string. |
|
toLocalString() |
convert
number to local string. |
|
toExponential() |
convert
decimal to exponential notation. |
|
toPrecision(1) |
convert
number to Precise . |
|
toPrecision(2) |
convert
decimal to precision 2 . |
|
toPrecision(3) |
convert
decimal to precision 3 . |
|
toFixed() |
to
convert a number to string with fixed decimal value |
|
String
to Number in JavaScript: JavaScript variables can be strings or numbers. Like var x="5"
is string,
but var y=5
is
number. Both can be used in arithmetic operations. Except addition, all
arithmetic operations are possible with x and y. Functions to convert string to number.
Number Function
- parseint Function
- parsefloat Function
Number
Function: Number Function can convert string to numbers. Number Function can convert both
floating/decimal and non-floating/integers. But if a string contains string
character like alphabets or special character, number function will return NaN.
var a = '100';var b = '100.5';var c = '100px';var d = 'abc100';
Number(a); //100Number(b); //100.5Number(c); //NaN - Not a NumberNumber(d); //NaN - Not a Number
var a = '100';var b = '100.5';var c = '100px';var d = 'abc100';
parseInt(a); //100parseInt(b); //100parseInt(c); //100parseInt(d); //NaN - Not a Number
parseInt function is also used to convert decimal to binary, decimal to octal and decimal to hexadecimal numbers. To do this, pass second parameter in parseInt function as 2 for binary, 8 for octal, and 16 for hexadecimal. See example.
parseFloat FunctionparseFloat Function can convert string to numbers, floating/decimals and
non-floating/intergers both. parseFloat can also convert binary, octal and hexadecimal
to decimal numbers.
var a = '100';var b = '100.5';var c = '100px';var d = '100.5px';var e = 'abc100';
parseFloat(a); //100parseFloat(b); //100.5parseFloat(c); //100parseFloat(d); //100.5parseFloat(e); //NaN - Not a Number
isNaN, is Not a Number Function
JavaScript isNaN function
returns a boolean value. For example, isNaN("2")
is
false and isNaN("a")
is
true. Even isNaN(NaN) is also true.
isNaN(NaN); // returns trueisNaN(1); // returns falseisNaN('1'); // returns falseisNaN('a'); // returns trueisNaN('2a'); // returns true
JavaScript isNaN function
returns a boolean value. For example, isNaN("2")
is
false and isNaN("a")
is
true. Even isNaN(NaN) is also true.
isFinite
isFinite function tells
whether a number is
finite or not. In JavaScript, number less than 1e308 or
1e+308 are finite numbers. Numbers greater than 1e308 are Infinite, for example,
2e308 and more are infinite numbers in JavaScript.
isFinite(Infinity); // falseisFinite(2e308); // falseisFinite(1e308); // trueisFinite(1e307); // trueisFinite(123); // true
- Never starts a number with 0.
- Floating numbers can have maximum 16 characters after decimal.
- Binary i.e.0b100 and Octal 0o100 are not supported in all browsers.
isFinite function tells whether a number is finite or not. In JavaScript, number less than 1e308 or 1e+308 are finite numbers. Numbers greater than 1e308 are Infinite, for example, 2e308 and more are infinite numbers in JavaScript.
- Floating numbers can have maximum 16 characters after decimal.
- Binary i.e.0b100 and Octal 0o100 are not supported in all browsers.