Javascript Data Types Flashcards

JavaScript variables can hold many data types: numbers, strings, objects and more:

1
Q

JavaScript Data Types
JavaScript variables can hold many data types: numbers, strings, objects and more:

var length = 16;                               // Number
var lastName = "Johnson";                      // String
var x = {firstName:"John", lastName:"Doe"};    // Object

In programming, data types are an important concept.
To be able to operate on variables, it is important to know something about the type.
Without data types, a computer cannot safely solve this:

var x = 16 + "Volvo";
Javascript reads the above as: var x = "16" + "Volvo";.
When adding a number and a string, JavaScript will treat the number as a string.

What code will produce “16Volvo” in the display?
How about “Volvo16” in the display?
Now if x is displayed as “20Volvo” with values of 16, 4, and Volvo?
How about “Volvo164” with values of Volvo, 16, and 4?

A
var x = 16 + "Volvo";
document.getElementById("demo").innerHTML = x;

var x = “Volvo” + 16;

var x = 16 + 4 + "Volvo";
document.getElementById("demo").innerHTML = x;
var x = "Volvo" + 16 + 4;
document.getElementById("demo").innerHTML = x;

In the first example, JavaScript treats 16 and 4 as numbers, until it reaches “Volvo”.

In the second example, since the first operand is a string, all operands are treated as strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
JavaScript Types are Dynamic
JavaScript has dynamic types. This means that the same variable can be used to hold different data types.
For example:
//Now x is undefined
//Now x is a Number
//Now x is a String
A

var x; // Now x is undefined
x = 5; // Now x is a Number
x = “John”; // Now x is a String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
JavaScript Strings
A string (or a text string) is a series of characters like "John Doe".

Strings are written with quotes. You can use single or double quotes: Volvo XC60 in single and double quotes with two different var names with a display of:

Volvo XC60
Volvo XC60

You can use quotes inside a string, as long as they don’t match the quotes surrounding the string:

// Double quote string
// Single quotes inside double quotes string
// Double quotes inside single quotes string
A
var carName1 = "Volvo XC60";
var carName2 = 'Volvo XC60';

document.getElementById(“demo”).innerHTML =
carName1 + “br>” +
carName2;

var answer1 = "It's alright";             // Single quote inside double quotes
var answer2 = "He is called 'Johnny'";    // Single quotes inside double quotes
var answer3 = 'He is called "Johnny"';    // Double quotes inside single quotes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

JavaScript Numbers
JavaScript has only one type of numbers.

Numbers can be written with, or without decimals:

var // Written with decimals
var // Written without decimals
Displays:
34
34
3.14
Extra large or extra small numbers can be written with scientific (exponential) notation:
// 12300000
// 0.00123
A
var x1 = 34.00;
var x2 = 34;
var x3 = 3.14;

document.getElementById(“demo”).innerHTML =
x1 + “br>” + x2 + “br>” + x3;

Extra large or extra small numbers can be written with scientific (exponential) notation:

var y = 123e5;      // 12300000
var z = 123e-5;     // 0.00123
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

JavaScript Booleans
Booleans can only have two values: true or false.

var x = 5;
var y = 5;
var z = 6;
(x == y)       // Returns true
(x == z)       // Returns false

What does the full code look like?

Booleans are often used in conditional testing.

A
script>
var x = 5;
var y = 5;
var z = 6;
document.getElementById("demo").innerHTML =
(x == y) + "br>" + (x == z);
/script>

Booleans are often used in conditional testing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

JavaScript Arrays
JavaScript arrays are written with square brackets.

Array items are separated by commas.

The following code declares (creates) an array called cars, containing three items (car names):

Array indexes are zero-based, which means the first item is [0], second is [1], and so on.

A

var cars = [“Saab”,”Volvo”,”BMW”];

document.getElementById(“demo”).innerHTML = cars[0];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

JavaScript Objects
JavaScript objects are written with curly braces {}.

Object properties are written as name:value pairs, separated by commas.
The object (person) in the example above has 4 properties: firstName, lastName, age, and eyeColor.

var person = {firstName:”John”, lastName:”Doe”, age:50, eyeColor:”blue”};

What would code look like displaying: John is 50 years old.

A
var person = {
  firstName : "John",
  lastName  : "Doe",
  age     : 50,
  eyeColor  : "blue"
};

document. getElementById(“demo”).innerHTML =
person. firstName + “ is “ + person.age + “ years old.”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The typeof Operator
You can use the JavaScript typeof operator to find the type of a JavaScript variable.

The typeof operator returns the type of a variable or an expression: What would code look like that returns 3 strings like this one on top of the other?

”” // Returns “string”
“John” // Returns “string”
“John Doe” // Returns “string”

Then what it looks like the typeof for these numbers?

0              // Returns "number"
314            // Returns "number"
3.14           // Returns "number"
(3)            // Returns "number"
(3 + 4)        // Returns "number"

The typeof operator returns the type of a variable or an expression.

A

document.getElementById(“demo”).innerHTML =
typeof “” + “br>” +
typeof “John” + “br>” +
typeof “John Doe”;

string
string
string

document.getElementById("demo").innerHTML = 
typeof 0 + "br>" + 
typeof 314 + "br>" +
typeof 3.14 + "br>" +
typeof (3) + "br>" +
typeof (3 + 4);
number
number
number
number
number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Undefined
In JavaScript, a variable without a value, has the value undefined. The type is also undefined.

var car; // Value is undefined, type is undefined

Any variable can be emptied, by setting the value to undefined. The type will also be undefined.

car = undefined; // Value is undefined, type is undefined

A

var car;
document.getElementById(“demo”).innerHTML =
car + “br>” + typeof car;

displays:
The value (and the data type) of a variable with no value is undefined.

undefined
undefined

var car = "Volvo";
car = undefined;
document.getElementById("demo").innerHTML =
car + "br>" + typeof car;

Variables can be emptied if you set the value to undefined.

undefined
undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Empty Values
An empty value has nothing to do with undefined.

An empty string has both a legal value and a type.

var car = “”; // The value is “”, the typeof is “string”

A
var car = "";
document.getElementById("demo").innerHTML =
"The value is: " +
car + "br>" +
"The type is: " + typeof car;

displays:
An empty string has both a legal value and a type:

The value is:
The type is: string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Null
In JavaScript null is “nothing”. It is supposed to be something that doesn’t exist.

Unfortunately, in JavaScript, the data type of null is an object.

You can consider it a bug in JavaScript that typeof null is an object. It should be null.

You can empty an object by setting it to null:

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null;    // Now value is null, but type is still an object

Then

You can also empty an object by setting it to undefined:

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = undefined;   // Now both value and type is undefined
A
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null;
document.getElementById("demo").innerHTML = typeof person;

displays:
Objects can be emptied by setting the value to null.

object

then

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = undefined;
document.getElementById("demo").innerHTML = person;

displays:
Objects can be emptied by setting the value to undefined.

undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Difference Between Undefined and Null
undefined and null are equal in value but different in type:

typeof undefined // undefined
typeof null // object

null === undefined // false
null == undefined // true

A
document.getElementById("demo").innerHTML =
typeof undefined + "br>" +
typeof null + "br>br>" +
(null === undefined) + "br>" +
(null == undefined);

displays:
Undefined and null are equal in value but different in type:

undefined
object

false
true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Primitive Data
A primitive data value is a single simple data value with no additional properties and methods.

The typeof operator can return one of these primitive types:

string
number
boolean
undefined

typeof “John” // Returns “string”
typeof 3.14 // Returns “number”
typeof true // Returns “boolean”
typeof false // Returns “boolean”
typeof x // Returns “undefined” (if x has no value)

A
document.getElementById("demo").innerHTML = 
typeof "john" + "br>" + 
typeof 3.14 + "br>" +
typeof true + "br>" +
typeof false + "br>" +
typeof x;

displays:
The typeof operator returns the type of a variable or an expression.

string
number
boolean
boolean
undefined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Complex Data
The typeof operator can return one of two complex types:

function
object
The typeof operator returns object for both objects, arrays, and null.

The typeof operator does not return object for functions.

typeof {name:’John’, age:34} // Returns “object”
typeof [1,2,3,4] // Returns “object” (not “array”, see note below)
typeof null // Returns “object”
typeof function myFunc(){} // Returns “function”

The typeof operator returns “object” for arrays because in JavaScript arrays are objects.

A
document.getElementById("demo").innerHTML = 
typeof {name:'john', age:34} + "br>" +
typeof [1,2,3,4] + "br>" +
typeof null + "br>" +
typeof function myFunc(){};

displays:
The typeof operator returns object for both objects, arrays, and null.

The typeof operator does not return object for functions.

object
object
object
function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly