Type conversion in JavaScript refers to the process of converting a value from one data type to another. JavaScript is a loosely typed language, which means that variables can hold values of any type, and type conversion can happen implicitly or explicitly.
Implicit Type Conversion(Coercion)
🙂Implicit type conversion happens automatically when an operation is performed on values of different types. 🙂JavaScript tries to convert one or both of the operands to a common type before the operation.
for example:-
var x = 5; // x is a number
var y = "10"; // y is a string
var result = x + y;
// JavaScript converts x to a string and performs string concatenation.
console.log(result); // Output: "510"
we will discuss string concatenation when we will discuss strings. But for reference, we can say that string concatenation simply means to add or combine two strings using the "+" operator.
Explicit Type Conversion (Type Casting)
Explicit type conversion, also known as type casting, occurs when a developer manually converts a value from one type to another using inbuilt functions like Number(), string(), parseInt(), and parseFloat().
parseInt(): Converts a string to an integer.
parseFloat(): Converts a string to a floating-point number.
string(): Converts a value to a string.
Number(): Converts a value to a number.
for example:-
var str = "123";
var num = parseInt(str); // Convert string to integer
console.log(num); // Output: 123
var floatStr = "3.14";
var floatNum = parseFloat(floatStr); // Convert string to float
console.log(floatNum); // Output: 3.14
var boolValue = true;
var strBool = String(boolValue); // Convert boolean to string
console.log(strBool); // Output: "true"
let strMarks = "90.5";
let marks = Number(strMarks); // convert string to number
console.log(marks); // output: 90.5
Trick 🤘 - conversion of number to string and string to number.
⭐To convert a number to a string without using built-in functions like string() is by concatenating the number with an empty string.
var number = 42;
var stringNumber = "" + number;
console.log(stringNumber); // Output: "42"
⭐To convert a string to a number without using built-in functions like Number()
is by using the unary plus (+) operator.
var stringNumber = "42";
var number = +stringNumber;
console.log(number); // Output: 42
Now we will discuss strings 🚀
In JavaScript, a string is a sequence of characters enclosed within single or double quotes. Strings are one of the primitive data types in JavaScript, and they are used to represent text.
Creating Strings:
You can create strings using single or double quotes-
let singleQuoted = 'Hello, World';
let doubleQuoted = "Hello, World";
Indexing
In JavaScript, strings are zero-indexed, which means that the first character in a string is at index 0, the second character is at index 1, and so on.
"Anjali Fname" --> A n j a l i space F n a m e
0 1 2 3 4 5 6 7 8 9 10 11
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
length of the string --> 12 (counting from 1)
You can access individual characters in a string using square brackets and the index of the character you want to retrieve.
var myString = "Hello, World!";
// Accessing characters by index
var firstChar = myString[0]; // 'H' (index 0)
var secondChar = myString[1]; // 'e' (index 1)
var sixthChar = myString[5]; // ',' (index 5)
console.log(firstChar, secondChar, sixthChar);
Length property
If you want to find the length of a string, you can use the length property
var myString = "Hello, World!";
var lengthOfString = myString.length; // 13
console.log(lengthOfString);
The length property of a string in JavaScript returns the number of characters in the string, including spaces and any other whitespace characters. It counts all characters, including letters, numbers, symbols, and spaces.
Keep in mind that attempting to access an index that is beyond the length of the string will result in undefined. For example:
var myString = "Hello, World!";
var outOfBoundsChar = myString[15]; // undefined
console.log(outOfBoundsChar);
In the next episode, we will talk about the methods of strings, string concatenation, and template strings. So, stay tuned for tomorrow's episode; it will be more interesting! ☺.
If you like my work, you can buy me a coffee and share your thoughts buymeacoffee.com/yashika227x