Aloysious' Codefellows Reading Notes
JavaScript / Conditionals / Operators / Data Types / Variable
:mortar_board: JAVASCRIPT MASTER CHEAT
Every written “instruction” is called a statement. JavaScript statements are separated by semicolons.
Statements: are the instructions within our program that get “executed” when the program runs. But! Not all JavaScript statements are “executed”. Any code after a double slash //, or between /* and */, is treated as a comment, and will be ignored, and not executed.
To write a Single line comment we use double slashes ( // ). Like this:
// This is a single line comment
alert(“This is an alert box!”);
To create a multi-line comment, we write it between /* and */
Like this:
/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
in my web page:
*/
document.getElementById(“myH”).innerHTML = “My First Page”;
document.getElementById(“myP”).innerHTML = “My first paragraph.”;
/*
document.getElementById(“myH”).innerHTML = “My First Page”;
document.getElementById(“myP”).innerHTML = “My first paragraph.”;
*/
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed
W3Schools Condional Statements
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Use the else statement to specify a block of code to be executed if the condition is false.
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Use the else if statement to specify a new condition if the first condition is false.
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Use the switch statement to select one of many code blocks to be executed.
Syntax
switch(expression) {
case x:
// code block
break; case y:
// code block
break; default:
// code block }
This is how it works:
Java divides the operators into the following groups:
Arithmetic operators are used to perform common mathematical operations.
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:
Example
int x = 10;
The addition assignment operator (+=) adds a value to a variable:
Example
int x = 10;
x += 5;
Comparison operators are used to compare two values:
Logical operators are used to determine the logic between variables or values:
Bitwise operators are used to perform binary logic with the bits of an integer or long integer.
JavaScript variables are containers for storing data values.
In this example, x, y, and z, are variables, declared with the var keyword:
Example
var x = 5;
var y = 6;
var z = x + y;
From the example above, you can expect:
x stores the value 5
y stores the value 6
z stores the value 11
Declare a varialble called x, assign the value 42 to it and output it to the colsole:
x = ___;
______.log(_____);
EXAMPLE
x = 42;
console.log(x);
A variable in Java must be a specified data type.
Data types are divided into two groups:
A primitive data type specifies the size and type of variable values, and it has no additional methods.
There are eight primitive data types in Java:
Data Type | Size | Description |
---|---|---|
byte | 1 byte | Stores whole numbers from -128 to 127 |
short | 2 bytes | Stores whole numbers from -32,768 to 32,767 |
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
boolean | 1 bit | Stores true or false values |
char | 2 bytes | Stores a single character/letter or ASCII values |
Non-primitive data types are called reference types because they refer to objects.
The main difference between primitive and non-primitive data types are:
Term | Context |
---|---|
JavaScript | JavaScript is the Programming Language for the Web. JavaScript can update and change both HTML and CSS. JavaScript can calculate, manipulate and validate data. |
conditionals | Conditional statements are used to perform different actions based on different conditions. |
operators | Operators are used to perform operations on variables and values. |
data types | a variable in Java must be a specified data type (two types: Primitive/ Non-Primitive) |
variable | JavaScript variables are containers for storing data values. |
Read