- Variable Syntax
- Input and
.value
- Data Types and Data Structures
- Methods
- Concatenation
- Mathematical Operators
- DOM Manipulation
- Event Handlers
- Conditionals
- Comparison Operators
- Logical Operators
- Arrays and Loops
.insertAdjacentHTML
You can declare and assign values to variables using let
, const
, or var
. let
is preferred when the value may change, while const
is used for unchangeable values.
let faveSeason = "summer"; // Declares a variable and gives it the name, faveSeason
faveSeason = "autumn"; // Assigns a different value to the variable faveSeason
let coldSeason = "winter"; // Declares a variable named coldSeason and assigns it a value "winter"
You can obtain values from HTML input fields using .value
in JavaScript.
<input class="username">
<button class="login">Go!</button>
button.onclick = function() {
let name = document.querySelector('.username').value; // Retrieves the value from the input field
};
JavaScript has several primitive data types, including numbers, strings, and boolean values. Also, arrays are objects that store multiple values.
let temperature = -1;
let greeting = "Joliz is here!";
let space = ' ';
let oddNumbers = [1, 3, 5, 7, 9];
JavaScript provides various built-in methods to perform actions, such as logging to the console, alerting, and converting data types.
console.log("Test"); // Logs "Test" to the console
alert("Password error!"); // Displays a pop-up alert box
let year = "2015";
Number(year); // Converts "2015" into the number 2015
You can join together or concatenate strings in JavaScript using the +
operator.
let userName = "codeNationStudent";
userList.insertAdjacentHTML("beforebegin", "<li> Hello " + userName + "</li>");
JavaScript provides arithmetic operators such as addition, subtraction, multiplication, and division.
let a = 5, b = 3;
let sum = a + b; // Addition
let difference = a - b; // Subtraction
let product = a * b; // Multiplication
let quotient = a / b; // Division
The Document Object Model (DOM) allows you to interact with and modify HTML content from JavaScript.
let paragraph = document.querySelector("p");
paragraph.innerHTML = "Hello!"; // Changes the text inside a <p> tag
You can define event handlers to respond to user actions like clicks, key presses, etc.
button.onclick = function() {
button.style = "color: blue"; // Changes the button text color to blue
};
JavaScript provides conditional statements like if
, else if
, and else
to control the flow of your program.
let num = 11;
if (num < 5) {
console.log("Less than 5");
} else if (num < 10) {
console.log("Less than 10");
} else {
console.log("Greater than 10");
}
Comparison operators like <
, >
, <=
, >=
, ==
, and ===
allow you to compare values.
if (number < 10) {
// ...
} else if (grade > 70) {
// ...
}
Logical operators like &&
, ||
, and !
allow you to create complex logical expressions.
if (age > 16 && passedTest === true) {
console.log("you can drive.");
} else {
console.log("no driving yet.");
}
Arrays store multiple values, and loops allow you to iterate through these values.
let classNames = ["English", "History", "Calculus"];
let arrayLength = classNames.length; // Returns 3
let newClass = "AP CS";
classNames.push(newClass); // Adds "AP CS" to the array
for (let arrayElement of classNames) {
// loop body goes here
}
This method allows you to insert HTML into the DOM at a specified position.
let groceries = ["Lettuce", "Tomatoes", "Milk"];
ul.insertAdjacentHTML("beforebegin", "<li>" + groceries[2] + "</li>");