JavaScript - WDD330📱
avaScript is one of the most used programming languages in the world, and JavaScript is responsible for the interactivity of the websites
Your First program in JavaScript Hello World
console.log('Hello World');
This code will be printed in your console Hello World, to add the Javascript in your HMTL you can add with the tag example of that as follows:
const btn = document.getElementById()
btn.addEventListener('click', function() {
alert('Hello World!');
};
you can call a JavaScript file example of that is as follows, you need to add the and the open and close tag of the script:
script src='js/helloWorld.js'
To create a comment a peace of test that the computer will not read the comment is design for programmer to organize the code or only explain any logic or objective for peace of
to comment we use//
or
/*comment*/
- The variable is a container for storing data including integer, float, strings, boolean,symbols, undefined and Null
- to use a variable you can use var, let, const
- var store any variable
- const store only constants variable that did not repeat
- let can store integers, strings and booleans.
Variables
Scope
- The scope determines the visibility of variables and makes contrast between variables, the The following example is for local scope.
function myFunction() {
let carName = "Volvo";
// code here CAN use carName
}
Arrays
- An array is a special variable, wich can hold more that one value.
const cars =['Ford','Volvo','Ferrari']
Logic Conditionals
- These logical conditions allow you to control the flow of a program.
- if Statement
- else Statement
- else if Statement
- Switch Statement
const age = 23;
if (age < 18) {
console.log('Sorry, you are not old enough to play this game');
}
const n = 12;
if (n%2 === 0) {
console.log('n is an even number');
} else {
console.log('n is an odd number');
}
if (number === 4) {
console.log('You rolled a four');
} else if (number === 5) {
console.log('You rolled a five');
} else if(number === 6){
console.log('You rolled a six');
} else {
console.log('You rolled a number less than four');
}
switch (number) {
case 4:
console.log('You rolled a four');
break;
case 5:
console.log('You rolled a five');
break;
case 6:
console.log('You rolled a six');
break;
default:
console.log('You rolled a number less than four');
break;
}
Loops
- Loops will repeat a pice of code until changing the conditions.
- While Loops
- do .. while Loops
- For Loops
let bottles = 10;
while (bottles > 0){
console.log(`There were ${bottles} green bottles, hanging on a wall. And if one green bottle should accidentally fall, there'd be ${bottles-1} green bottles hanging on the wall`);
bottles--;
}
do {
do something
} while(condition)
for (initialization ; condition ; after) { do something }
Functions
- A function is a chunk of code that you can reuse when you call, the function reduce repetition of code.
- Invoking a Function.
function hello(){
console.log('Hello World!');
}
hello();
'Hello world!'
Questions?
What is the difference between functions and arrow functions in what scenario you can use it?
JavaScript is not a class-based object-oriented language. But you still have ways to use object-oriented programming (OOP).
Contact me
📧Email me at iodaniel@byui.edu
- Lesson 01: Notes
- Lesson 02:
- Lesson 03:
- Lesson 04:
- Lesson 05:
- Lesson 06:
- Lesson 07:
- Lesson 08:
- Lesson 09:
- Lesson 10:
- Lesson 11: