No history yet

JavaScript Fundamentals

The Building Blocks

In JavaScript, we store information in variables. Think of them as labeled boxes where you can keep different kinds of data. To declare a variable, you use keywords like let, const, or the older var. It's best to use let for variables whose value might change and const for variables that should never change, also known as constants.

```javascript
// Use 'let' for variables that can be reassigned.
let userAge = 25;
userAge = 26; // This is fine.

// Use 'const' for constants that never change.
const birthYear = 1998;
// birthYear = 1999; // This would cause an error!

// 'var' is an older way to declare variables. 
// It has some tricky behaviors, so 'let' and 'const' are preferred.
var score = 100;

Variables can hold different types of data. JavaScript has several fundamental data types, often called primitive types. Understanding these is key because the type of data determines what you can do with it.

Data TypeDescriptionExample
StringA sequence of text characters.'Hello, world!'
NumberAny number, including integers and decimals.42 or 3.14
BooleanRepresents a logical value of true or false.true
UndefinedA variable that has been declared but not assigned a value.let name;
NullRepresents the intentional absence of any object value.let user = null;

Anything that isn't one of these primitive types is considered an object. This includes more complex data structures like arrays (lists of items) and even functions themselves.

Making Things Happen

Once you have data in variables, you need a way to work with it. That's where operators come in. They are symbols that perform specific operations.

Arithmetic operators perform math. They're straightforward and work just like you'd expect.

let sum = 10 + 5; // 15 (Addition)
let difference = 10 - 5; // 5 (Subtraction)
let product = 10 * 5; // 50 (Multiplication)
let quotient = 10 / 5; // 2 (Division)
let remainder = 10 % 3; // 1 (Modulus - finds the remainder)

Assignment operators are used to assign values to variables. The basic one is the equals sign (=), but there are shortcuts for modifying a variable's existing value.

let level = 5;
level += 2; // Equivalent to: level = level + 2; (level is now 7)
level -= 3; // Equivalent to: level = level - 3; (level is now 4)

Comparison operators compare two values and return a boolean (true or false). These are crucial for making decisions in your code. A key distinction to remember is between == and ===.

Always use the strict equality operator (===) for comparisons. It checks both the value and the data type, which prevents common bugs.

```javascript
5 === 5; // true
5 === '5'; // false (different types)

5 == '5'; // true (loose equality - best to avoid)

10 > 5; // true (Greater than)
10 !== 5; // true (Strictly not equal)

Logical operators are used to combine multiple boolean expressions.

```javascript
let isMember = true;
let hasCoupon = false;

// AND (&&): both must be true
isMember && hasCoupon; // false

// OR (||): at least one must be true
isMember || hasCoupon; // true

// NOT (!): inverts the boolean value
!isMember; // false

Controlling the Flow

Your code doesn't have to run from top to bottom, every single line, every single time. Control structures allow you to create logic that makes decisions or repeats actions. The most common way to make a decision is with an if...else statement.

```javascript
let temperature = 75;

if (temperature > 80) {
  console.log('It is hot outside.');
} else if (temperature > 60) {
  console.log('It is pleasant outside.');
} else {
  console.log('It is cold outside.');
}
// Output: It is pleasant outside.

When you need to perform the same task over and over, you use a loop. The for loop is perfect when you know exactly how many times you want to repeat an action.

// This loop runs 5 times
for (let i = 1; i <= 5; i++) {
  console.log('This is loop number ' + i);
}

The while loop is better suited for when you want to loop as long as a certain condition is true, but you don't know ahead of time how many iterations that will be.

```javascript
let count = 0;

while (count < 3) {
  console.log('Still counting...');
  count++;
}
// This will print 'Still counting...' three times.

Reusable Code Blocks

Functions are one of the most important concepts in programming. They are reusable blocks of code that perform a specific task. You define a function once and can call it, or invoke it, as many times as you need. Functions can take inputs, called parameters, and can return an output value.

```javascript
// Function definition with two parameters: name and age
function greet(name, age) {
  const message = 'Hello, ' + name + '! You are ' + age + ' years old.';
  return message;
}

// Calling the function and storing the returned value
let greeting = greet('Alice', 30);

console.log(greeting); 
// Output: Hello, Alice! You are 30 years old.

Related to functions is the concept of scope. Scope determines where your variables are accessible. Variables declared inside a function are in that function's local scope and can't be seen from the outside. Variables declared outside any function are in the global scope.

Finally, there's a behavior in JavaScript called hoisting. In simple terms, JavaScript 'hoists' or moves declarations for variables and functions to the top of their scope before the code is executed. For functions, this means you can call a function before you've actually written it in your code. For variables declared with var, their declaration is hoisted, but their assignment is not. This can lead to confusing behavior, which is another reason let and const are now preferred, as they are hoisted differently and provide a 'temporal dead zone' that throws an error if you try to access them before they are declared.

// Function hoisting works as expected
sayHello(); // Works!

function sayHello() {
  console.log('Hello!');
}

// Variable hoisting with 'var'
console.log(myVar); // Outputs: undefined
var myVar = 10;

// With 'let', this would throw a ReferenceError, which is clearer.
// console.log(myLet); // ReferenceError: Cannot access 'myLet' before initialization
// let myLet = 10;

With these fundamental pieces, you can start building any logic you can imagine. They are the essential toolkit for every JavaScript developer.