Data TypesDescriptionExample 4represents textual data 5, 6 etc 7an integer or a floating-point number 8, 9, 0 etc. 1an integer with arbitrary precision 2 , 3 etc. 4Any of two values: true or false 5 and 6 7a data type whose variable is not initialized 8 9denotes a 9 value 1 2data type whose instances are unique and immutable 3 4key-value pairs of collection of data 5 Show Here, all data types except 4 are primitive data types, whereas 4 is non-primitive.Note: The 4 data type (non-primitive type) can store collections of data, whereas primitive data type can only store a single data.JavaScript String 4 is used to store text. In JavaScript, strings are surrounded by quotes:
For example,
Single quotes and double quotes are practically the same and you can use either of them. Backticks are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with 3 as shown above.You will learn about the use of backticks in the JavaScript String tutorial. JavaScript Number 7 represents integer and floating numbers (decimals and exponentials). For example,
A number type can also be 5, 6, and 7 (not a number). For example,
JavaScript BigIntIn JavaScript, 7 type can only represent numbers less than (253 - 1) and more than -(253 - 1). However, if you need to use a larger number than that, you can use the 1 data type.A 1 number is created by appending n to the end of an integer. For example,
Output 900719925124740999n Uncaught TypeError: Cannot mix BigInt and other types Note: 1 was introduced in the newer version of JavaScript and is not supported by many browsers including Safari. Visit to learn more.JavaScript BooleanThis data type represents logical entities. 4 represents one of two values: 5 or 6. It is easier to think of it as a yes/no switch. For example,
You will learn more about booleans in the JavaScript Comparison and Logical Operators tutorial. JavaScript undefinedThe 7 data type represents value that is not assigned. If a variable is declared but the value is not assigned, then the value of that variable will be 7. For example,
It is also possible to explicitly assign a variable value 7. For example,
Note: It is recommended not to explicitly assign 7 to a variable. Usually, 9 is used to assign 'unknown' or 'empty' value to a variable.JavaScript nullIn JavaScript, 9 is a special value that represents empty or unknown value. For example,
The code above suggests that the number variable is empty. Note: 9 is not the same as NULL or Null.JavaScript SymbolThis data type was introduced in a newer version of JavaScript (from ES2015). A value having the data type 2 can be referred to as a symbol value. 2 is an immutable primitive value that is unique. For example, 0Though value1 and value2 both contain 5, they are different as they are of the 2 type.Visit JavaScript Symbol to learn more. JavaScript ObjectAn 6 is a complex data type that allows us to store collections of data. For example, 1You will learn about JavaScript Objects in later tutorials. JavaScript TypeJavaScript is a dynamically typed (loosely typed) language. JavaScript automatically determines the variables' data type for you. It also means that a variable can be of one data type and later it can be changed to another data type. For example, 2JavaScript typeofTo find the type of a variable, you can use the 7 operator. For example, 3Notice that 7 returned 9 for the 9 type. This is a known issue in JavaScript since its first release.
How do you declare variables in types?The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.
How to declare variable type string in JavaScript?To declare variables in JavaScript, you need to use the var, let, or const keyword. Whether it is a string or a number, use the var, let, or const keyword for its declaration. But for declaring a string variable we had to put the string inside double quotes or single quotes.
|