Skip to main content
Web MobileBeginner12 min readUpdated March 2025

JavaScript Fundamentals

JavaScript powers the web, servers and mobile apps. Learn variables, functions, arrays, objects and the modern ES6+ syntax every developer uses daily.

Variables and Data Types

Use `const` by default and `let` when a value must change. Avoid `var`, which has confusing scoping rules.

JavaScript has primitive types (string, number, boolean, null, undefined, bigint, symbol) and objects (including arrays and functions).

javascript
const name = "Ada";
let score = 10;
score += 5;

const user = { name, score, tags: ["admin", "beta"] };
const { tags } = user;            // destructuring
const [first, ...rest] = tags;    // array destructuring + rest

console.log(`${name} has ${score} points`);
console.log(typeof user, Array.isArray(tags), first, rest);

Functions and Arrow Functions

Functions are first-class values: you can store them in variables, pass them as arguments and return them from other functions. Arrow functions give a short syntax and do not rebind `this`.

javascript
function add(a, b = 0) {
  return a + b;
}

const double = (n) => n * 2;

const makeCounter = () => {
  let count = 0;
  return () => ++count;   // closure keeps "count" alive
};

const next = makeCounter();
console.log(add(2, 3), double(4), next(), next());

Working with Arrays

Prefer the declarative array methods over manual loops. They are concise and make intent clear:

javascript
const products = [
  { name: "Keyboard", price: 80, inStock: true },
  { name: "Monitor", price: 240, inStock: false },
  { name: "Mouse", price: 30, inStock: true },
];

const available = products.filter((p) => p.inStock);
const names = available.map((p) => p.name);
const total = available.reduce((sum, p) => sum + p.price, 0);
const cheapest = [...products].sort((a, b) => a.price - b.price)[0];

console.log(names, total, cheapest.name);

Equality and Truthiness

Always use strict equality (`===`) to avoid surprising type coercion. Remember the falsy values: `false`, `0`, `""`, `null`, `undefined` and `NaN`. Use optional chaining (`?.`) and nullish coalescing (`??`) for safe defaults.

javascript
const settings = { theme: null, retries: 0 };

console.log(settings.theme ?? "dark");   // "dark"
console.log(settings.retries ?? 3);      // 0  (|| would wrongly give 3)
console.log(settings.profile?.email);    // undefined, no error

Key Takeaways

  • Use const by default, let when needed, and never var.
  • Functions are values; closures let them remember their surrounding scope.
  • map, filter and reduce make array code short and readable.
  • Use === plus ?. and ?? to avoid coercion and null errors.

Contact Us

Have a question or feedback? Fill out the form below or reach us directly at support@nvaitraining.com