Introduction
JavaScript is a lightweight, cross-platform, and interpreted compiled programming language with first-class functions.
While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as Node.js.
Key features of JavaScript include:
- Imperative and declarative capabilities
- Dynamic typing syntax
- Prototype-based object orientation
Variables
You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.
You can declare a variable in three ways:
var x = 42;- Declares a variable, optionally initializing it to a value.let y = 13;- Declares a block-scoped local variable.
Data Types
The latest ECMAScript standard defines eight data types, including seven Primitive types and Objects.
Primitives include Boolean, Null, Undefined, Number, BigInt, String, and Symbol.
Example of string declaration:
const greeting = "Hello World";
Functions
Functions are one of the fundamental building blocks in JavaScript. A function is a JavaScript procedure-a set of statements that performs a task.
To declare a function, use the function keyword followed by a name:
function square(number) {
return number * number;
}
Control Flow
JavaScript supports a compact set of statements, specifically control flow statements, that you can use to incorporate interactivity.
Use the if statement to execute a statement if a logical condition is true:
if (condition) {
statement_1;
} else {
statement_2;
}