javaScript Variables: types of variables
2 min readFeb 8, 2021
As we know, we can assign all sorts of different types to a variable. But javaScript has types.
In particular, it provides primitive types and object types.
— Primitive Types
- number
- string
- boolean
- symbol
— and two special types
- null
- undefined
Lets understand them in detail.
Numbers.
- A numeric literal is a number represented in the source code, amd depending on how it’s written, it can be an integer literal or a floating point literal.
integers:
30
5354576
0xCB //hex
floats:
3.14
.124
5.2e4 //5.2 * 10^4
Strings.
- A string type is a sequence of characters.
- It’s defined in the source code as a string literal, which is enclosed in quotes or double quotes
'A string'
"Another string"//Strings can span across multiple lines by using the backslash"A \
string" //Strings can be joined using the + operator:"A " + "string" // string concatenation//A string can contain escape sequences that can be interpreted when the string is printed, like \n to create a new line.'I\'m a developer'
Booleans.
- JavaScript defines two reserved words for booleans which are: true and false.
- Many comparision operations
==
===
<
>
(and so on) return either one or the other.
0
-0
NaN
undefined
null
'' //empty string
null.
null
is a special value that indicates the absence of a value.- It’s a common concept in other languages as well, can be known as
nil
orNone
in Python for example.
undefined.
undefined
indicates that a variable has not been initialized and the value is absent.- It is commonly returned by functions with no
return
value. - When a function accepts a parameter but that is not set by the caller, it is undefined
//To detect if a value is undefined, you use the construct:typeof variable === 'undefined'
Object Types
- Anything that is not primitive type is an object type.
- Object types have properties and also have methods that can act on those properties.
typeof 10=== 'number'
typeof '10' === 'string'
typeof {name: 'superman'} === 'object'
typeof [1, 2, 3, 4] === 'object'
typeof false === 'boolean'
typeof undefined === 'undefined'
typeof (() => {}) === 'function'// Any variable has a type assigned. Use the typeofoperator to get a string representation of a type:
hope this helped you understand the different types of Variables in javaScript. If you have any questions please drop me a text. 😄