Introduction to Data Types and Variables in JS
A variable could store any type of data. It could be one of the primitive type or a complex type of a class.
Javascript allows you to define dynamic type. You can define first as string data type than it can be assigned to a different data type value (in the below example it is number data type value)
Let's review the data types.
Undefined
Undefined data type has only one value which is undefined. Variable is assigned to undefined value when it is not set to value after it is defined
Null
Null data type has only one value which is null. Javascript returns true if you compare Null and undefined equavialent
A number (integer)
The following code is an example of integer data type
A number (float)
The following code is an example of float data type
NaN
Nan means not a number. It is a data type which states it is an invalid number.
Any operation with Nan datatype returns as NaN
It returns as false when you compare any value with NaN even including itself
A string
String data type containe zero or more characters. Strings could be defined with the following charachters ", ', ` . There is no difference on what you used when you define the string.
To add number data type into a string variable ends up with concanating variables with string type.
Embeding an expression
var expression1 = `2 + 3 equals to ${2 + 3}`
The expression inside ${…} is executed and the result becomes a part of the string. We can put anything in there: a variable like name or an arithmetical expression like 2 + 3 or something more complex. Please note that this can only be done in backticks. Other quotes don’t have this embedding functionality! alert( "the result is ${1 + 2}" ); // the result is ${1 + 2} (double quotes do nothing)
A boolean
The boolean type has only two values. It is whether true or false.
Boolean values also come as a result of comparisons:
var isGreater = 4 > 1;
console.log( isGreater ); // true (the comparison result is "yes")
- Comment Your JavaScript Code
- Declare Variables
- Data Types
- Type Conversions
- + Plus Operator
- - Minus Operator
- * Multiply Operator
- * Multiply Operator (1)
- % Modulus (Division Remainder) Operator
- ** Exponentiation Operator
- Bitwise operators
- Comparisons
- Logical Operators
- Interaction: alert, prompt, confirm, console.log
- The "switch" statement
- Loops: while, do while and for