-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_2_basic_types.js
More file actions
111 lines (76 loc) · 4.26 KB
/
javascript_2_basic_types.js
File metadata and controls
111 lines (76 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//------------------------------------------------------------------------------
// JavaScript variables can hold many data types: numbers, strings, objects
// and more:
var weapon = "Mystical Blade of Awesomeness"; // A String.
var price = 1000; // A Number.
//------------------------------------------------------------------------------
// JavaScript has 5 primitive data types. A primitive value is a simple data
// value with no additional properties and methods.
//
// string - A variable that is a series of characters like "Game Over".
// number - A number value like 42 or 3.14.
// boolean - A variable that can be true or false.
// undefined - A variable without a value, has the value undefined.
// null - In JavaScript, null means "nothing". It is supposed to be something that
// doesn't exist. Unfortunately, in JavaScript, the data type of null is
// an object.
// You can use the JavaScript typeof operator to find the type of a
// JavaScript variable.
console.log(typeof "Game Over!"); // Prints "string"
console.log(typeof 42); // Prints "number"
console.log(typeof 3.14); // Prints "number"
console.log(typeof undefined); // Prints "undefined"
console.log(typeof null); // Prints "object" (I know... this doens't make any sense!)
//------------------------------------------------------------------------------
// JavaScript is dynamicly typed. This means that the same variable can be used
// to hold different data types:
var x; // x is not set to any value so it is undefined.
var x = 42; // Now x is a Number
var x = "John"; // Now x is a String
//------------------------------------------------------------------------------
// A string (or a text string) is a series of characters like "Game Over".
// Strings are written with quotes. You can use single or double quotes:
var gameOverMessage = "Game Over"; // Using double quotes
var gameOverMessage = 'Game Over'; // Using single quotes
// You can use quotes inside of a string, as long as they don't match the quotes
// surrounding the string:
var playerDiedMessage = "It's lights out Bro!"; // A single quote used inside a double quoted string
var playerDiedMessage = "Player 'Kewl Dude' was killed!"; // Single quotes used inside a double quoted string
var playerDiedMessage = 'Player "Kewl Dude" was killed!'; // Double quotes used inside a single quoted string
//------------------------------------------------------------------------------
// JavaScript has only two types of numbers: Integers and Decimals:
var score = 4200; // Integers do not have any fractional part or decimal values.
var headShotAccuracy = 88.72; // Decimals can have fractional parts.
// Very large or small numbers can be written with scientific or
// exponential notation:
var ammoRemaining = 123e5; // 12300000
var sizeOfTargetOnEnemyBoss = 123e-5; // 0.00123
//------------------------------------------------------------------------------
// Booleans can only have two values: true or false.
var thisGameRocks = true;
var playerFoundMagicKey = false;
//------------------------------------------------------------------------------
// JavaScript arrays are written with square brackets with the array items
// separated by commas.
// The following code creates an array called levelNames, which contains the names
// of 5 levels for a game:
var levelNames = ["Village of Hostile Peasants", "Forest of Bats", "Swamp of Despair", "Cave of Woes", "Castle of the Enemy Boss"];
// We can access the array values stored at each index like so:
console.log(levelNames[0])
console.log(levelNames[1])
console.log(levelNames[2])
console.log(levelNames[3])
console.log(levelNames[4])
//------------------------------------------------------------------------------
// JavaScript objects are written with curly braces.
// Object properties are written as name:value pairs, separated by commas.
var player1 = {
name:"Kewl Dude",
numKills:250,
favWeapon:"Plasma Rifle in the 40 Watt Range"
};
// We can access the object's properties like so:
console.log("Player's Name: " + player1.name)
console.log("Number of Kills: " + player1.numKills)
console.log("Favorite Weapon: " + player1.favWeapon)
// https://www.w3schools.com/js/js_datatypes.asp