Open your Browser's Console: F12
Ctrl + Shift + J / Cmd + Shift + J
document.write( "Hello JS!" );
document.write( "HTML is allowed also!
" );
- Internal <head><script>
- Internal </body><script>
- External <script src="app.js">
( app.js is just a text file with a .js extension )
- Inline (Should be avoided!)
Strings (Text)
Numbers
Booleans (true, false)
What are Comments?
Parts of the program that don't get executedSingle line comments:
// This is a single line comment
Multi-line comments:
/* Comments that
span multiple lines
can be placed like this.
*/
https://bit.ly/2U97u52
*In JavaScript there are 2 basic functionsalert( VALUE );
console.log( VALUE );
"2" + 2;
"22" // String
2 + "2";
"22" // String
"2" - 1;
1 // Number
2 - "2";
0 // Number
"A" - 2;
NaN
let strValue = "2";
let numValue = 2;
let floatValue = "3.1415";
Number( strValue );
//=> 2
parseInt( strValue, 10 );
//=> 2
parseFloat( floatValue, 10 );
//=> 3.1415
String( numValue );
//=> "2"
""+2
//=> "2"
( A dive into our mobile phone's
contacts mechanism )
(We'll also see multiple entries per contact in the next slides.)
https://bit.ly/2AOczXA
https://bit.ly/2Ue8btL
https://bit.ly/2KQCumh
...and come back to our mobile contact list example
with additional entries per contact!
What's the use?
They help us switch our code
and add conditional logic
( Drawing Board / Conditionals Chart )
Image by: Guru99
if { ... }
let trafficLight = 'red';
if ( trafficLight === 'red' ){
console.log( "Stop the car!" );
}
if { ... } else { ... }
let trafficLight = 'red';
if ( trafficLight === 'red' ){
console.log( "Stop the car!" );
} else {
console.log( "Move on." );
}
NESTED if { ... } else { ... }
let trafficLight = 'red';
if ( trafficLight === 'red' ){
console.log( "Stop the car!" );
} else {
if ( trafficLight === "orange" ){
console.log( "Slow down!" );
} else {
if ( trafficLight === "green" ){
console.log( "Move on." );
}
}
}
This can be written like this...
if { ... } else if { ... }
if ( trafficLight === 'red' ){
console.log( "Stop the car!" );
} else if ( trafficLight === "orange" ){
console.log( "Slow down!" );
} else if ( trafficLight === "green" ){
console.log( "Move on." );
}
No more else ... if's!
let trafficLight = "orange";
switch (trafficLight){
case "red":
console.log("Stop!");
break;
case "orange":
console.log("Slow down");
break;
case "green":
console.log("Move on");
break;
default:
console.log("Default catch...");
}
while ( EXPRESSION ){
RUN CODE...
}
While some EXPRESSION evaluates to TRUE run the { code }
let counter = 0;
while ( counter < 3 ){
console.log( "Counter value is: " + counter );
counter = counter + 1;
}
Output:
Counter value is: 0
Counter value is: 1
Counter value is: 2
let counter = 0;
while ( counter++ < 3 ){
console.log( "Counter value is: " + counter );
}
Output:
Counter value is: 1
Counter value is: 2
Counter value is: 3
References:
let counter = 0;
while ( counter < 3 ){
console.log( counter );
counter = counter + 1;
}
for ( let counter = 0; counter < 3; counter = counter + 1 ){
console.log( counter );
}
So, the loop structure is this:
for ( START STATEMENT; CONDITION; END STATEMENT ){
CODE
}
let someArray = [ "a", "b", "c" ];
for ( let counter = 0; counter < someArray.length; counter++ ){
console.log( "Counter value is: " + someArray[counter] );
}
Output:
Counter value is: a
Counter value is: b
Counter value is: c
Declaring a Function:
function repeatCode( param, otherParam ){
console.log( param );
console.log( otherParam );
}
Calling the Function:
repeatCode( "value for param", "value for otherParam" );
Will output:
"value for param"
"value for otherParam"
Declaring a Function (#2):
function repeatCode( param, otherParam ){
console.log( param );
console.log( otherParam );
}
Can be declared as:
let repeatCode = function( param, otherParam ){
console.log( param );
console.log( otherParam );
}
The return statement:
function repeatCode( one, two ){
return one + two;
}
repeatCode( 4, 6 );
console.log( repeatCode( 4, 6 ) );
let sum = repeatCode( 4, 6 );
console.log( sum );
( What if there is no return statement present? )
let arr = [ "Kosmas", "Kostas", "Chris" ];
console.log( arr[0] ); // "Kosmas"
Under the hood, this works much like this:
[ 0: "Kosmas", 1: "Kostas", 2: "Chris" ]
Try it on your console!
Objects as Variable Collections
let model = "Nokia";
let color = "Black";
let type = "X5";
Organize:
{
model : "Nokia",
color : "Black",
type : "X5",
}
Pack:
let nokiaPhone = {
model : "Nokia",
color : "Black",
type : "X5",
}
Declaring an Object:
let obj = {
teacher: "Kosmas",
assistant: "Kostas",
founder: "Chris"
}
In Objects, instead of using a numeric index for each element,
we explicitly define a key (String).
Accessing an Object Property:
let obj = { teacher: "Kosmas", assistant: "Kostas", founder: "Chris" }
console.log( obj["teacher"] );
console.log( obj.assistant );
Will Output:
"Kosmas"
"Kostas"
Setting an Object Property:
let obj = { teacher: "Kosmas", assistant: "Kostas", founder: "Chris" }
obj.teacher = "Mary";
console.log( obj );
console.log( obj["teacher"] );
Will Output:
{ teacher: "Mary", assistant: "Kostas", founder: "Chris" }
"Mary"
When an Object Property contains a Function, it is called a method
let car = {
color: "red",
year: 2010,
type: "sports",
showInfo: function(){
console.log("A red sports car.");
}
}
car.showInfo();
Will output:
"A red sports car."
You can access any Object property using the this keyword, while inside the Object.
let car = {
color: "red",
year: 2010,
type: "sports",
showInfo: function(){
console.log("A " + this.color + " sports car.");
}
}
car.showInfo();
Will output:
"A red sports car."
An Object is a collection of variables (called properties)
using the . dot
let client = {
age: 50,
name: "Jane",
surname: "Doe",
getName: function(){
console.log( this.name + " " + this.surname );
}
}
client.getName();
"Jane Doe"
Strings
Numbers
Booleans
null
undefined
(Primitives)
Arrays
Objects
Functions
Google: mdn js keyword
Tech Instructor
Full Stack Developer @ https://plethorathemes.com