cover-img

JavaScript Data Types

20 January, 2023

25

25

3

Data Types

JavaScript has several data types that you can use in your code. These data types include:

  1. String: A string is a sequence of characters, enclosed in either single or double quotes. For example: "Hello" or 'Hello'
  2. Number: A number can be an integer or a floating-point value. Unlike other languages, JavaScript does not have separate data types for different kinds of numbers, such as integers, short, long, or floating-point.
  3. Boolean: A boolean data type can only have two values: true or false.
  4. Null: The null data type represents an absence of a value or a null reference.
  5. Undefined: The undefined data type is a value that is assigned to a variable that has not been assigned a value.
  6. Object: An object is a data type that represents a collection of properties. Each property has a name and a value, and the values can be of any data type.
  7. Symbol: A symbol is a data type introduced in ECMAScript 2015 that is used to create unique identifiers for objects.

JavaScript also has several other data types, such as arrays and functions, which are considered subtypes of objects. We’ll start looking at using these data types further along in the Series.

Type Conversion

Type conversion in JavaScript refers to the process of converting a value from one data type to another. This can be done either implicitly or explicitly.

Implicit type conversion occurs when an operator or function is applied to values of different data types, and JavaScript automatically converts the values to the appropriate data type for the operation. It is known as Type Coercion in JavaScript.

For example:


let x = 10;
let y = "5";

console.log(x + y); // Output: "105"

In the example above, the value of y is a string, but the + operator is being used for addition. JavaScript will implicitly convert the number 10 to the string "10" and perform the concatenation, resulting in a final value of 105.

Explicit type conversion, also known as type casting, involves explicitly converting a value to a specific data type using type conversion functions. For example:


let x = "10";
let y = 5;

console.log(Number(x) + y); // Output: 15
console.log(String(y) + x); // Output: "510"

In the example above, the Number() function is used to explicitly convert the string "10" to the number 10, and the String() function is used to explicitly convert the number 5 to the string "5".


25

25

3

ShowwcaseHQ

San Francisco, CA, USA

Showwcase is where developers hang out and find new opportunities together as a community

More Articles