JavaScript + CSS M3L1
JavaScript + CSS M3L1
LESSON 1
Introduction
to JavaScript
Methodological
Скачать Guidelines
методичку
MODULE 3. LESSON 1
New Topic:
Introduction to JavaScript
JavaScript
To start using JavaScript, we need to answer 3 questions:
1. What is an algorithm?
2. What is a programming language?
3. What is a program?
Algorithm —
is a sequence of actions determined to achieve a goal.
Programming language —
is a language for communicating with machines, in our case —
with the browser.
Internet. Continuation
numbers: add, multiply, divide, subtract. Everything is
New material:
5 + 5
10 - 7
12 / 4
3 * 9
Working with numbers
All the rules of arithmetic work here too. For example, the result
Internet. Continuation
of the following operations will be predictable:
New material:
(2 - 7) * (-5)
By the way!
Internet. Continuation
impossible to obtain its result in the form of a value.
New material:
Variables
You can put a number into a variable:
Internet. Continuation
The structure of the
let number = 5
New material:
let result = (5 + 5) * 7
Variables
To create a variable, you must:
Internet. Continuation
● Use the let keyword.
New material:
let number = 5 let result = (5 + 5) * 7
Meaning
Title Title Meaning
Keyword Keyword
Variables
The = operator is used to assign a value. There are also
operators for changing existing values: +=, -=, *=, /=. Here is
an example:
let number = 10
The value to change to
number += 5
Internet. Continuation
calculations:
New material:
let b = 20
let result = a * b
Internet. Continuation
● You can use letters from English alphabet, numbers,
New material:
● The variable name cannot start with a digit.
Output of the result
The result variable will store the number 200. But how
will we know about it?
console.log(result)
console.log()
You can pass the value of a variable into the
console.log() brackets, as we just did,
and also the whole arithmetic expression. Then its
result will be calculated first, and only then it will appear
in the console:
let a = 6
console.log(a * 5)
alert()
The alert() command is another way to see
the data. It works exactly the same as console.log(),
but the result comes up in the
pop-up window, not in the console.
let a = 10
let b = 20
let result = a * b
alert(result)
alert()
In this case the value of expression is again calculated
first:
let a = 6
alert(a * 5)
Don't you think
there's something
missing?
It seems we don't
know how to
output text :(
10:4
5
Data Types
Obviously, we need an opportunity to work
with text in JavaScript. In fact, it's not that difficult: the
only rule is that the text must always be in
quotation marks. Here, for example, is Fred's greeting
in the console:
let a = '5'
let b = ' cacti in the basket'
console.log(a + b)
Data Types
Strings and numbers in JavaScript are different data
types, and operations with them are carried out by the
different rules. If you add several number together, the
result is obvious. But if you add strings:
console.log(a + b)
Data Types
Data type of the values is a top priority for JavaScript
while executing each command.
Here the result will be the string “104”: the number has
turned into a string so that it's possible for addition operator
to work.
Data Types
JavaScript figured everything out for us! But what if we
wanted to get a number after all? In case a string needs
to be considered a number, a unary plus is used. We
put it before the string value — we get a number:
let a = 10
let b = '4'
console.log(a + +b)
Data Types
JavaScript figured everything out for us! But what if we
wanted to get a number after all? In case a string needs
to be considered a number, a unary plus is used. We
put it before the string value — we get a number:
let a = 10
let b = '4' This time we'll get the number 14.
console.log(a + +b)
To summarize:
1. How can JavaScript store data?
2. Which ruled must be followed while giving a name to
a variable?
3. Is it possible to put the quotient of two other
variables into a variable?
4. Explain what a data type is in your own words
5. Which operations can be performed with numbers,
and which ones with strings?
Well done! Let's
move on ;)
10:2
1
MODULE 3. LESSON 1
New topic.
Interaction
with a page
Changing the page
As you'll remember from the beginning of the class,in JS you can
dynamically change the state of the page.
Have you got any idea how the JS code is related to the HTML
code of the page and the objects on it?
Changing the page
As we remember from the beginning of the class, in JS you can
dynamically change the state of the page. JavaScript code is
linked to the HTML code of the page through the DOM tree.
DOM — Document Object Model — is the Object Model Of The
Document.
DOM tree
By default, when loading a page, the browser builds a DOM tree
with all the page elements, their contents, attributes and
properties. Each node of the tree is an object reflecting a
specific element from the page.
DOM tree, example
document
<!DOCTYPE html>
<html>
html
<head>
<meta charset='UTF-8'>
<title>DOM tree</title>
</head>
<body> head body
<div class='container'>
<h2>List of products</h2>
<ul>
<li>Indoor cactus</li> meta title .container
<li>Garden cactus</li>
<li>Small cactus</li>
<li>Pink cactus</li>
</ul> ul h2
</div>
</body>
</html>
li li li li
DOM tree
Before changing an element, the browser must first locate it
among other DOM objects. So you should first specify which
element to look for. The plan is as follows:
If there are several elements with the same selector, you can
search not across the entire tree, but only within a specific
element (among its descendants).
Note: the += operator also works here — the new line will be added to the old content.
Changing the elements
second.style.color = 'red'
Before: After:
First paragraph Now there’s another text
Second paragraph Second paragraph
Other styles
You can change any styles, and all of them are referred to just
like in CSS. The exception is properties that in CSS have a dash in
the name. For example, font-size or margin-top:
second.style.fontSize = '20px'
second.style.marginTop = '20px'
That's it!
Pinning
● How do you get an element by its class?
● Is it possible to get an element by it's tag?
● Which of the elements will be obtained if there are several
elements with the specified selector?
● Is it possible to change the style of an object?
● Which command allows you to change the contents of an
object?