JavaScript is a programming language primarily used to add interactivity and functionality to web pages. Here is a brief explanation of some of the basic syntax of JavaScript:
Variables:
JavaScript uses the keyword "var" to declare variables. Variables are used to store data that can be used later in the program. For example:
var name = "John";
var age = 25;
Functions:
Functions are blocks of code that perform a specific task. They can take in inputs (parameters) and return outputs. For example:
function addNumbers(num1, num2) {
return num1 + num2;
}
Conditional statements:
Conditional statements are used to make decisions in the code based on certain conditions. The two most common conditional statements in JavaScript are "if" and "switch". For example:
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}
Loops:
Loops are used to repeat a block of code multiple times. The two most common loops in JavaScript are "for" and "while". For example:
for (var i = 0; i < 5; i++) {
console.log(i);
}
while (i < 10) {
console.log(i);
i++;
}
Arrays:
Arrays are a useful tool for storing several values in one variable. For example:
var fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // Outputs "apple"
JavaScript Keyword:
In JavaScript, a keyword is a reserved word that has a special meaning and purpose in the language. These keywords cannot be used as identifiers (variable names, function names, etc.) because they are already predefined in the language and have specific functionality associated with them.
Here are some examples of JavaScript keywords:
- var - used to declare a variable
- function - used to declare a function
- if - used to start a conditional statement
- else - used to provide an alternative block of code to run if the if statement is false
- for - used to create a loop
- while - used to create a loop that runs as long as a specified condition is true
- break - used to exit a loop or switch statement
- continue - used to skip the current iteration of a loop and move to the next one
- return - used to return a value from a function.
It's important to be familiar with these keywords and their proper usage when writing JavaScript code.
Whitespace:
Whitespace refers to any space, tab, or line break that appears between two characters in the code. Whitespace is generally used to improve the readability of code and make it easier to understand.
Here are some examples of whitespace in JavaScript:
Example 1: Using whitespace to separate keywords and expressions
if (x > 5) {
console.log("x is greater than 5");
}
In this example, a whitespace exists between the if keyword, the opening parenthesis (, the variable x, the comparison operator >, the number 5, the closing parenthesis ), and the opening curly brace {. This makes the code easier to read and understand.
Example 2: Using whitespace to format code blocks
function addNumbers(num1, num2) {
let result = num1 + num2;
return result;
}
In this example, there is whitespace between the function name addNumbers, the opening parenthesis (, the parameters num1 and num2, the closing parenthesis ), the opening curly brace {, the variable declaration let result = num1 + num2; and the return statement. This formatting makes the code block easier to read and understand.
Example 3: Using whitespace to separate array elements
let myArray = [1, 2, 3, 4, 5];
In this example, there is a whitespace between the elements of the array myArray. This makes it easier to visually separate the elements and understand the contents of the array.
Overall, the use of whitespace in JavaScript is important for improving code readability and making it easier to understand.
Previous:
Setup JavaScript Development Environment