As a front-end developer, having a solid foundation of JavaScript syntax and functions is essential. In this article, we'll cover over 100 commonly-used JavaScript functions and syntax, with detailed explanations and practical examples.
Basic Syntax
Variables
Variables are used to store data in JavaScript. There are three keywords to declare a variable: var
, let
, and const
.
// Declare a variable using var var myVar = "Hello World"; // Declare a variable using let (block-scoped) let myLet = "Hey there!"; // Declare a variable using const (also block-scoped) const myConst = "Hi!";
Data Types
JavaScript has six primitive data types: string
, number
, boolean
, null
, undefined
, and symbol
. Objects are another type in JavaScript.
-- -------------------- ---- ------- -- ------ --- -------- - ------ ------- -- ------ --- --- - --- -- ------- --- ------ - ----- -- ---- --- ------ - ----- -- --------- --- ------------ -- ------ ----- -------- - --------- -- ------ --- -------- - - ----- ------- ---- -- --
Operators
Operators are used to perform actions on variables and values. Some common operators in JavaScript include:
-- -------------------- ---- ------- -- ---------- --------- --- - - -- --- - - -- ------------- - --- -- - ------------- - --- -- - ------------- - --- -- -- ------------- - --- -- --- ------------- - --- -- - -- ---------- --------- ------------- - --- -- ---- ------------- - --- -- ----- ------------- -- --- -- ---- ------------- -- --- -- ----- ------------- -- --- -- ----- ------------- -- --- -- ---- -- ------- --------- --- --------- - ----- --- -------- - ------ --------------------- -- ---------- -- ----- --------------------- -- ---------- -- ---- ----------------------- -- ----
Control Flow
Control flow statements are used to control the order in which statements are executed. Common control flow statements include if-else
, for
, while
, and switch
.
-- -------------------- ---- ------- -- ------- --------- --- --- - --- -- ---- -- --- - ---------------- --- -- --------- - ---- - ---------------- --- - --------- - -- --- ---- --- ---- - - -- - - -- ---- - --------------- - -- ----- ---- --- ----- - -- ----- ------ - -- - ------------------- -------- - -- ------ --------- --- ----- - -------- ------ ------- - ---- -------- ---------------- ----- -- --------- ------ ---- --------- ---------------- ----- - ---------- ------ -------- ------------------- -- ----- ---- ---- --------- -
Functions
Functions are blocks of code that can be called to perform a specific task. They can also accept parameters and return values.
Function Declaration
function sayHello() { console.log("Hello!"); } sayHello(); // "Hello!"
Function Parameters
function greet(name) { console.log(`Hello, ${name}!`); } greet("John"); // "Hello, John!"
Function Return Values
function add(x, y) { return x + y; } let sum = add(2, 3); console.log(sum); // 5
Arrow Functions
Arrow functions are a concise way to write functions in JavaScript.
-- -------------------- ---- ------- -- -------- ----------- -------- -------------- - ------------------- ----------- - -- ----- -------- ----- ----- - ------ -- - ---------------- ----------- --
Higher-Order Functions
Higher-order functions are functions that accept other functions as arguments or return functions as values.
function multiplyBy(factor) { return function (number) { return number * factor; }; } const double = multiplyBy(2); console.log(double(5)); // 10
Arrays
Arrays are used to store collections of data in JavaScript. They can be of any data type and can be accessed by index.
Array Declaration
let myArray = ["apple", "banana", "orange"];
Accessing Array Elements
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/2000