Data Type: String
The String data type in JavaScript is used to represent textual data. A string represents an array of characters. Each element in the array occupies a position in the array. The first element is at index 0, the next at index 1, and so on. The length of the String (length of a string) is the number of elements (characters) in it.
Unlike, for example, the C language, strings in JavaScript are immutable (immutable), which means that once they are created, they cannot be changed. However, it is possible to create a new string based on the execution of the following operations on the existing string by:
- Extracting individual characters by selecting individual letters or using the .substr() method;
- Merging multiple strings by concatenation using the (+) operator or using the .concat() method;
3.1.1. Extracting individual characters in a string
Let's say we have a declared variable text, initialized to the value "Sentence text", If you wanted to access the character within the string at index 1, you would do it in the following way:
text[1];
If you want to access a larger number of characters, you can use the .substring() function, so that the first argument in the function specifies the starting index, and the second, the number of characters you need to extract.
tekst.substring(0, 4);
In the above example, the first four characters starting from the first will be printed. This means that characters at indexes 0, 1, 2, and 3 will be printed.
var text = "Sentence text"; console.log(text[1]); // The console will print „e“ console.log(text.substring(0, 4)); // The console will print „Sent“
3.1.2. String length
To check how many characters there are in a particular string or the length of the string, it is possible to call the .length property on the string in the following way.
var myVariable = "Hello World!!!"; console.log(myVariable.length); // 14
3.1.3. Concatenating strings (concatenation)
Concatenation, or connecting strings, can be done using the plus (+) operator or by calling the .concat(); method on a string.
var text = "Concatenating" + " " + "strings";
// It will print „Concatenating strings in JavaScript.“
var concat1 = text + " " + "in" + " " + "JavaScript.";
var concat2 = text.concat(" ", "in", " ", "JavaScript.");
var secondText = "Joining";
var firstWord = "strings";
var secondWord = "concatenation.";
// It will print „Joining strings by concatenation.“
var concat3 = secondText.concat(firstWord, " ", secondWord);
3.1.4. String Interpolation
String interpolation is a method used in JavaScript to embed expressions within string literals. It's a convenient way to create a new string by including variables, expressions, or code snippets within a string.
In JavaScript, string interpolation is achieved using template literals. Template literals are string literals that allow embedded expressions, and they are enclosed by backticks (` `) instead of single or double quotes.
Syntax:
const name = "Alice";
const greeting = `Hello, ${name}!`;
console.log(greeting); // Output: Hello, Alice!
In this example, The string is enclosed in backticks (` `) , in which variables are substituted. ${name} embeds the value of name into the string.
Data Type: Number
According to the ECMAScript standard, there is
var myVariable = "Text"; console.log(myVariable + 2); // NaN (Not-a-Number)
only one type of number in JavaScript, which are decimal numbers between -(253-1) and 253-1. Numbers in JavaScript are called "Number", and they may or may not have decimal points. If they don't have decimals, they are treated as integers. Furthermore, JavaScript does not differentiate between number precisions. For instance, in C#, there are multiple number types (int, int32, float, double, etc.), while JavaScript has only one numerical data type.
In terms of precision, numbers in JavaScript are equivalent to the double data type in C# and Java.
Every data type defines its literal values, including numbers. In JavaScript, the following number literals are available:
- Whole numbers: These are written like any other whole number - 125, -16, 0, 5, 128
- Decimal numbers: These are written with a dot as the decimal separator - 0.006, 12.256, -2.8
- Exponential notation: The number (argument) is written first, followed by the "e" symbol and the power of 10. Examples: 10e2 = 10 * 102 = 10 * 100 = 1000; 6e3 = 6 * 103 = 6 * 1000 = 6000
- Hexadecimal notation: Written with the "0x" prefix, followed by the hexadecimal number. For instance, 0x1a is 26 in hexadecimal notation, and -0xcc is -204 in hexadecimal notation.
- Octal notation: Written with the "0" prefix, followed by the octal number. For instance, 011 is 9 in octal notation, and -031 is -25 in octal notation.
If you write numbers in octal notation, you need to be careful not to unintentionally write incorrect values. For example, if you write -31, the result will be -31, but the result from -031 will be -25.
In the next example, variables are declared and initialized with the previously mentioned numerical types.
<script>
var positiveInt = 7; // Positive integer
var negativeInt = -125; // Negative integer
var positiveDecimal = 5.5; // Positive decimal number
var negativeDecimal = -0.256; // Negative decimal number
var expNotation = 6e2; // Number in exponential notation
var positiveHex = 0x1a; // Positive hexadecimal number
var negativeHex = -0xcc; // Negative hexadecimal number
var positiveOctal = 011; // Positive octal number
var negativeOctal = -031 // Negative octal number
</script>
In addition to these representations of numbers, the Number data type has three symbolic values:
- +Infinity – Positive infinity
- –Infinity – Negative infinity
- NaN (not-a-number) – The value is not a number
For example, if you try to divide and print 32 by positive zero, you would get positive infinity (Infinity), while dividing by negative zero would return negative infinity (-Infinity).
console.log(32 / +0); // Infinity console.log(32 / -0); // -Infinity
If you put something other than a number as an argument, it cannot be converted to a number, and the NaN (Not-a-Number) data type value will be returned.
Let's say we have a variable declared that contains a string (text) data type, and we try to perform an additional operation on it. The returned value will be NaN.
Data Type: Boolean
The Boolean data type can only contain two values: true or false.
var x = true; var y = false;
They are most commonly used for conditional testing, which you will learn about later in this manual.
Data Type: Null
In JavaScript, null means "nothing" and should represent something that does not exist. Unfortunately, in JavaScript, the data type of null values is object, which can be considered a mistake in JavaScript.
In the following example, the person variable is set to a null value.
var person = null; // The value is null, but the data type is still an object
Data Type: Undefined
In JavaScript, variables without assigned values have the value undefined. The data type is also undefined in this case.