2.0 To bring JS file
<body>
<script src="app.js"></script>
</body>
Console -> JS
Style -> CSS
Element -> HTML
2.1 Basic data types
javascript has type, and understand it automatically
1) Number (Integer) = Full number, Float (eg. 0.5)
2) Text (string) -> Always with " "
2.2~2.3 Variables
In VScode, app.js
console.log(value/string);
-> print something(value/string) on your console
Variable : Save or Hold Value.
To create variable
1) Const (Constant : Non changing)
const a = 5; ( Created a variable 'constant' and the name is 'a' and 'a' is equal with 5)
2) let
use when I want to create, and it can be changed.
2.4 Boolean (True/False/Null/undefined)
const amIFat = false; (This cannot be string)
null means nothing. null is used to tell the browser that this valuse is nothing.
undefined means something is in memory but it doesn't have value.
2.5 Arrays (to list elements)
Rule1. you have to use [ ] at the begining and end.
Rule2. Every single items should be seperated by ','
If you need to point specific element
const daysOfWeek = ["mon", "tue", "wed", "thu", "fri", "sat"]
-> console.log(daysOfWeek[n-1]);
If you want to add one more day to the array
daysOfWeek.push("sun")
If you want to update to the array
daysOfWeek[2] = "Tuesday"
and console again.
2.6 Objects
Rule1. you have to use { } at the begining and end.
Rule2. inside of { }, you can write the codes like name: "nico"
and put the ',' after property. if theres another one.
const player = {
name: "nico",
points: 10,
fat: true,
};
2.7~2.8 Functions
function name_of_function(){content}
eg.
function sayHello() {console.log("Hello!");
Argument
-> while youre pressing/playing function,
you can also send some information to the function
<Calculator>
const calculator = {
plus: function (a, b) {
console.log(a+b)
},
minus: function (a, b) {
console.log(a-b)
},
powerof: function (a, b) {
console.log(a**b)
},
divide: function (a, b) {
console.log(a/b)
},
};
calculator.plus(2, 3)
calculator.minus(2, 3)
calculator.powerof(2, 3)
calculator.divide(2, 3)
2.10~2.11 Recap
2.11 Returns
<Calculator>
const calculator = {
plus: function (a, b) {
return(a+b)
},
minus: function (a, b) {
return (a-b)
},
powerof: function (a, b) {
return (a**b)
},
divide: function (a, b) {
return (a/b)
},
};
once you write return -> the function stops working.
2.13 Conditionals (If/else)
1. old way
const age = prompt("stirng")
prompt is going to ask user showing the message to input a value
eg. const age = prompt("How old are you?");
console.log(age);
2. modern way
parseInt-> converse string to number
console.log(typeof "15", typeof parseInt("15"));
console.log(age,parseInt(age));
2.14 Conditionals part2.
if(condition {
condition = true
} else {
condition = false
}
*condition has to be a boolean and
has to evaluate to a boolean
eg.
const age = parseInt(prompt("How old are you?"));
if(inNaN(age){
console.log("Please write a number")}
else { console.log("Thank you for writing your age")}
2.14 Conditionals part3.
To make multiple conditions. <else if>
if(isNaN(age){
console.log("Please write a number");
} else if (age < 18) {
consol.log("You are too young");
} else {
console.log("You can drink");
if(isNaN(age){
console.log("Please write a number");
} else if (age < 18) {
consol.log("You are too young");
} else if(age>=18 && age <=50){
consol.log(" You can drink ");
} else {
console.log("You can't drink");
&& (and) / || (or)
if(isNaN(age) || age<0){
console.log("Please write a real positive number");
} else if (age < 18) {
consol.log("You are too young");
} else if(age>=18 && age <=50){
consol.log(" You can drink ");
} else {
console.log("You can't drink");
'Java Script' 카테고리의 다른 글
To do list (1) (0) | 2024.08.28 |
---|---|
Quotes (0) | 2024.08.27 |
Clock (0) | 2024.08.26 |
Login Form (0) | 2024.08.23 |
Javascript on the browser (0) | 2024.08.22 |