Javascript - Variables and DOM
Javascript - Variables and DOM
Javascript
What is JavaScript
1. A high-level definition
1. JavaScript is a programming language that allows you to implement complex
things on the web.
2. JavaScript is a programming language that enables you to create dynamically u
pdating content, control multimedia, animate images, and pretty
much everything else.
2. What can it really do
1. Store useful values inside variables.
2. Operations on pieces of text.
3. Running code in response to certain events occurr
ing on a web page
4. And much more!
Application Programming Interfaces (APIs)
- Not built into the browser by default, and you generally have to grab their code and
information from somewhere on the Web.
- The Twitter API allows you to do things like displaying your latest tweets on your website.
- The Google Library allows you to embed custom maps into your website, and other
such functionality.
- The Facebook API allows you to interact with facebook application data.
- Many other...
How do you add JavaScript to your page
- Modules
<script type=”module”></script>
Variables
- Declaring a variable tells the JavaScript to reserve memory space for the
variable.
- To declare JavaScript variable, use the statement
let hi
where hi is the name assigned to the variable.
- To declare variable and set its initial value, use
let variables = value;
where value is the initial value of the variable.
Declaring variables
<script>
let position = “Front-end developer”;
let industry = “IT - Web Development”;
console.log(position);
console.log(industry);
</script>
Variable types
1. Numeric values
2. Text (Strings)
3. Boolean values
4. Null value
5. Undefined
6. * You must declare variable before you can use it.
Variable types
2. document.getElementsByTagName("p")
3. document.getElementsByClassName("hi")
4. document.querySelectorAll("h3 .intro")
5. document.querySelector(".example")
Dom Manipulation
Example 1
DOM – Changing elements
1. document.createElement(element)
2. document.body.removeChild(element)
3. document.body.appendChild(element)
4. oldElement.parentNode.rеplaceChild(oldElement, newElement)
5. document.write(text)
Hello World in Javascript
● Open up the JavaScript Console in your browser, type the following, and
press ENTER:
- console.log(“HELLO WORLD”)
- document.write("Welcome to Brainster Javascript Courses")
- alert("How are you today?")
Exercise
have value
“Hello from Javascript”.
◈ New DIV should be placed before the existing
div.