Essential JavaScript Concepts: Data Types, Operators, Functions, and More
Build a strong foundation in JavaScript with this comprehensive guide to fundamental concepts. This tutorial covers data types, operators, functions, control flow, and common techniques, providing a solid base for writing efficient and effective JavaScript code.
Essential JavaScript Concepts: Data Types, Operators, and More
This guide covers fundamental JavaScript concepts, including data types, operators, functions, and some common techniques. Understanding these building blocks is essential for writing effective JavaScript code.
Data Types in JavaScript
JavaScript has two main categories of data types:
- Primitive Data Types
- Non-Primitive (Reference) Data Types
Primitive Data Types
These types hold a single value.
- String: A sequence of characters (e.g., `"hello"`, `'JavaScript'`).
- Number: Numeric values (e.g., `10`, `3.14`, `-5`).
- Boolean: Logical values (`true` or `false`).
- BigInt: Arbitrary-precision integers (for numbers exceeding the limits of the standard `Number` type; use the `n` suffix: `12345678901234567890n`).
- Undefined: Represents a declared variable that has not been assigned a value.
- Null: Represents the intentional absence of a value.
- Symbol: Unique and immutable values (introduced in ES6).
The `typeof` Operator
The `typeof` operator determines a variable's data type. Note that `typeof null` returns "object", and `typeof NaN` returns "number".
typeof 10; // "number"
typeof "hello"; // "string"
typeof true; // "boolean"
typeof null; // "object"
Non-Primitive (Reference) Data Types
These types can store multiple values or complex data.
- Object: A collection of key-value pairs (properties). Keys are strings; values can be any data type.
- Array: An ordered list of values. Elements can be accessed by their index (starting from 0).
Operators in JavaScript
Comparison Operators (`==` vs. `===`)
The `==` operator checks for loose equality (values only), while `===` checks for strict equality (values and types must match).
Dynamic HTML and Text Generation
JavaScript's `innerHTML` property lets you dynamically write HTML content into an HTML element, and `innerText` writes plain text:
document.getElementById('myDiv').innerHTML = "<p>Hello!</p>"; //Adds HTML
document.getElementById('myDiv').innerText = "This is text"; //Adds plain text
Creating Objects and Arrays
You can create objects using object literals or the `new Object()` constructor. Arrays are created using array literals or the `new Array()` constructor.
// Object literal
let myObj = { name: "Alice", age: 30 };
// Array literal
let myArray = [1, 2, 3];
`isNaN()` Function
The `isNaN()` function checks if a value is "Not a Number":
isNaN("hello"); // true
isNaN(10); // false
String Concatenation and Arithmetic
The `+` operator performs both string concatenation and addition, depending on the operands. If one operand is a string, it does concatenation; otherwise, addition.
Client-Side vs. Server-Side JavaScript
Client-side JavaScript runs in a web browser and interacts directly with the webpage's elements. Server-side JavaScript runs on a server (e.g., using Node.js).
Cookie Storage
Cookie storage locations vary depending on the operating system and browser.
`event.preventDefault()` vs. `event.stopPropagation()`
(The original text mentions `event.preventDefault()` and `event.stopPropagation()`. Since this section is incomplete, please refer to the original document for this explanation.)