JavaScript 01 · JavaScript in general
Variables, types and operators
const and let, numbers and text, type conversion, === vs. == and useful string methods.
Loading lesson…
JavaScript 01 · JavaScript in general
const and let, numbers and text, type conversion, === vs. == and useful string methods.
Loading lesson…
People learning React often find that it isn't React that trips them up, but the JavaScript around it. React itself is just a handful of functions – everything else you write in a component is plain JavaScript.
It's like cooking from recipes. Recipes (React) are quick to learn – as long as you can chop, fry and weigh (JavaScript). If you can't chop, every recipe is a struggle. So the first eight lessons are a kind of knife skills course: general JavaScript, no React. The last two lessons of this part then show how these skills are used in React.
A variable is a named box for a value. In modern JavaScript you create one with two keywords:
const name = 'Anna' // const = the box can't be refilledlet count = 0 // let = you can put another value in the box latercount = count + 1 // ✅ let can be reassigned: take the old value, add 1 and store it back → 1count += 1 // ✅ the same, shorter ("add 1 to count") → 2count++ // ✅ even shorter, can only do +1 → 3name = 'Peter' // ❌ TypeError: Assignment to constant variable.
The table compares three ways of creating a variable. The third one, var, is the old way you'll see in tutorials from before 2015 – what "valid in the block" means is shown in the very next part.
| const | let | var | |
|---|---|---|---|
| Can be reassigned? | no | yes | yes |
| Valid in | the block { … } | the block { … } | the whole function (confusing) |
| Use it? | yes, the default choice | when the value changes | no, it's outdated |
A variable only exists inside the curly braces { … } it was created in. Outside, nobody knows about it:
const price = 100if (price > 50) { // if = "when the condition holds, run the block" – more in JavaScript 06const discount = 10 // only exists inside this blockconsole.log(price - discount) // 90 – you can see outer variables inside too}console.log(discount) // ❌ ReferenceError: discount is not defined
userName, totalPrice, isLoggedIn.is, has, can, should: isOpen, hasError.users), a single item in the singular (user).getUser, formatPrice, handleClick.Every value in JavaScript has a type – is it text, a number, a yes/no…? Primitive types are simple values (one text, one number), everything else is an object – a value that holds other values inside. typeof is an operator (a word that does something with a value, much like +) that returns the type of a value as text.
The table also shows things that only come later – don't worry, it's enough to know they exist: NaN and Infinity are explained in section 3, objects { … } inJavaScript 03, arrays [ … ] in JavaScript 04, functions in JavaScript 02 and the date new Date() in JavaScript 08. A boolean is a type with only two values: true (yes) and false (no).
| Type | Examples | typeof returns |
|---|---|---|
| string (text) | 'hello', "hello", `hello` | 'string' |
| number | 42, 3.14, -1, NaN, Infinity | 'number' |
| boolean | true, false | 'boolean' |
| undefined | "no value has been set yet" | 'undefined' |
| null | "intentionally empty" | 'object' ⚠️ a historical bug |
| object | { name: 'Anna' }, [1, 2], new Date() | 'object' |
| function | () => {} | 'function' |
typeof 'hello' // 'string'typeof 42 // 'number'typeof true // 'boolean'typeof undefined // 'undefined'typeof { a: 1 } // 'object'typeof [1, 2] // 'object' – an array is an object too!typeof null // 'object' – a bug from 1995 that never went awayArray.isArray([1, 2]) // true – the right way to recognise an array: "is this an array?"value === null // the right way to recognise null (value = the variable you're checking; === in section 6)
JavaScript has a single type, number, for both whole and decimal numbers. Decimals are written with a dot (3.14). Math is a set of maths functions built into JavaScript – you call them with a dot, e.g. Math.round(2.5), and put the numbers they should work with in the parentheses.
10 + 3 // 1310 - 3 // 710 * 3 // 3010 / 3 // 3.333333333333333510 % 3 // 1 – the remainder after division (modulo)2 ** 10 // 1024 – powerMath.round(2.5) // 3 – roundsMath.floor(2.9) // 2 – downMath.ceil(2.1) // 3 – upMath.trunc(-2.9) // -2 – cuts off the decimal part (floor would give -3: down = towards the smaller number)Math.abs(-5) // 5 – the absolute value: the number without its minus signMath.min(4, 1, 9) // 1 – the smallest of the numbersMath.max(4, 1, 9) // 9 – the largest of the numbersMath.random() // a random number from 0 (inclusive) to 1 (exclusive)
// (=== means "is it equal?" and returns true or false – more in section 6)7 % 2 === 1 // true – 7 ÷ 2 = 3, remainder 1 → an odd number8 % 2 === 0 // true – 8 ÷ 2 = 4, remainder 0 → an even numberconst seconds = 187Math.floor(seconds / 60) // 3 – whole minutesseconds % 60 // 7 – the remaining seconds → "3:07"index % 3 // 0, 1, 2, 0, 1, 2… – taking turns in a cycle (colours, say)// (index = the item's position: 0, 1, 2, 3, 4…)
0.1 + 0.2 // 0.30000000000000004 😱0.1 + 0.2 === 0.3 // false(0.1 + 0.2).toFixed(2) // '0.30' – to 2 decimal places, but CAREFUL, returns TEXTMath.round((0.1 + 0.2) * 100) / 100 // 0.3 – a number rounded to 2 places
The last line rounds to two places in three steps: times 100 moves the decimal point two places to the right (30.000000000000004), Math.round rounds to a whole number (30) and divided by 100 moves it back (0.3).
A computer stores numbers in binary and can't write 0.1 in it exactly – just as we can't write 1/3 exactly as a decimal. That's why money is often counted in cents (whole numbers) and converted to dollars or euros only when it's displayed.
JavaScript writes numbers "the programmer's way": with a decimal point and nothing between the thousands. For someone reading the page that's unusual. The toLocaleString method writes a number the way it's written in a given language:
const price = 1234.5price.toFixed(2) // '1234.50' – no thousands separator: the way JavaScript writes itprice.toLocaleString('en-US') // '1,234.5' – a comma between thousands: the way an American writes itprice.toLocaleString('de-DE') // '1.234,5' – a dot between thousands, a decimal comma: the way a German writes it// 'en-US' = a language and country code: English (en) in the USA (US)
Just like toFixed it returns text, not a number – use it only for display and keep calculating with the original number. Prices with a currency symbol and always two decimal places, percentages or dates can be formatted with Intl, which comes in lesson JavaScript 08.
Number('hello') // NaN – Number(…) tries to turn a text into a number (more in section 5); 'hello' can't be0 / 0 // NaNNaN === NaN // false – NaN isn't even equal to itself!Number.isNaN(NaN) // true – the only reliable test10 / 0 // Infinity (not an error)
In programming, a piece of text is called a string – a string of characters one after another. You can write it in single '…', double "…" or back `…` quotes (the backtick key is usually left of 1). Backticks have a superpower: you can put a value inside.
const name = 'Anna'const count = 3'Hello, ' + name + '! You have ' + count + ' messages.' // the old way – easy to forget a space`Hello, ${name}! You have ${count} messages.` // template – ${…} inserts the value`Total: ${count * 12} USD` // any expression can go inside ${…} → 'Total: 36 USD'`Status: ${count > 0 ? 'you have some' : 'empty'}` // condition ? if yes : if no (more in JavaScript 06)const html = `<ul><li>A template can span several lines</li></ul>`
A method is a function that belongs to a value – you call it with a dot and parentheses: text.trim() means "take the text and trim it". Sometimes you put what the method should work with in the parentheses: s.includes('World'). Strings can't be changed – every method returns a new text and leaves the original alone.
const text = ' Hello World 'text.length // 15 – the number of characters (spaces included)text.trim() // 'Hello World' – without spaces at the endstext.toUpperCase() // ' HELLO WORLD 'text.toLowerCase() // ' hello world 'const s = 'Hello World's[0] // 'H' – the character at position 0 (counting starts at zero!)s.at(-1) // 'd' – the last character (a negative number counts from the end: -1 = the last)s.includes('World') // true – does it contain 'World'?s.startsWith('Hello') // true – does it start with 'Hello'?s.endsWith('!') // false – does it end with an exclamation mark?s.indexOf('W') // 6 – where it starts; -1 when not founds.slice(0, 5) // 'Hello' – from position 0 to 5 (5 not included)s.slice(6) // 'World' – from position 6 to the ends.slice(-5) // 'World' – the last 5 characterss.replace('World', 'Anna') // 'Hello Anna' – replaces the FIRST occurrence'a-b-c'.replaceAll('-', '/') // 'a/b/c' – replaces all of them'Anna Smith'.split(' ') // ['Anna', 'Smith'] – splits into an array (an array = a list of values, JavaScript 04)['a', 'b', 'c'].join(', ') // 'a, b, c' – the opposite: joins an array into text'7'.padStart(2, '0') // '07' – pads on the left to length 2'ha'.repeat(3) // 'hahaha'
// Every character has its own number (code) in the computer; < and > compare those numbers'a' < 'b' // true – compares by character codes'Z' < 'a' // true 😕 capital letters have smaller codes'é' > 'f' // true 😕 accented letters come after the English alphabet'é'.localeCompare('f', 'fr') // -1 – "é comes before f" according to French ✅'Anna'.toLowerCase() === 'anna' // comparison that ignores letter case
localeCompare compares two texts by the rules of a language ('fr' = French) and returns a number: negative when the first text comes before the second in the alphabet, 0 when they're the same, and positive when it comes after. You'll use it mostly for sorting lists (lesson JavaScript 05).
Everything a user types into a form arrives as text. Even when they type "42". If you want to calculate with it, you have to convert it. Number(…), String(…) and Boolean(…) are built-in functions that turn the value in the parentheses into a number, a text and a yes/no:
Number('42') // 42Number('3.14') // 3.14Number('3,14') // NaN – a comma isn't a decimal point!Number('') // 0 – careful, empty text is zeroNumber(' 7 ') // 7 – spaces at the ends don't matterNumber('12px') // NaNparseInt('12px') // 12 – reads digits from the left as long as it can (int = integer, a whole number)parseFloat('3.5kg') // 3.5 – the same with the decimal part (float = a decimal number)parseInt('3.9') // 3 – only the whole partString(42) // '42'(42).toString() // '42' – parentheses around the number, or JS reads the dot as a decimal point42 + '' // '42' – works, but String() is easier to readBoolean('hello') // true – non-empty text is "true-ish"Boolean('') // false – empty text is "false-ish" (more in JavaScript 06)!'hello' // false – ! is negation: it turns a "true-ish" value into false and back!!'hello' // true – a double negation (flips it twice) is a shortcut for Boolean()
When JavaScript gets an unexpected type, it tries to convert it on its own. The results can be surprising:
'5' + 3 // '53' – plus with text JOINS texts'5' - 3 // 2 – minus only works with numbers, so it converts the text'5' * '2' // 10true + 1 // 2[] + [] // '' – [] is an empty array; JS converts even that "somehow"'10' > '9' // false 😱 compares texts character by character: '1' < '9'10 > 9 // true
What you see: A text field and, below it, what different conversions make of its contents. This is exactly how values arrive from forms – always as text.
Try it:
text + 1 gives '421' (joined texts), but text * 1 gives the number 42 – multiplication converts the text to a number.Number returns NaN, parseFloat just 3. Only replacing the comma with a dot helps.Number gives up, parseInt reads 12.Number('') is 0, but Boolean('') is false. And '' == 0 is even true.The takeaway: Always convert form text yourself and check it with Number.isNaN. Relying on automatic conversion is a road to bugs.
Loading the interactive part…
Two equals signs (==) convert types before comparing, following complicated rules. Three equals signs (===) compare both the type and the value – no surprises.
| Expression | == (loose) | === (strict) |
|---|---|---|
5 vs '5' | true | false |
0 vs '' | true | false |
0 vs false | true | false |
null vs undefined | true | false |
NaN vs NaN | false | false |
5 === 5 // true5 === '5' // false – a different type5 !== '5' // true – "not equal" in the strict versionconst age = 30age > 18 // true – greater thanage < 18 // false – less thanage >= 30 // true – greater than or equalage <= 29 // false – less than or equalage < 65 && age >= 18 // true – && means "and at the same time": both hold (more in lesson JavaScript 06)
Every comparison returns a boolean – true or false. Those are exactly the answers that conditions (if) use, which come in lesson JavaScript 06.
Two really short functions to warm up: a greeting with a name and double a number. What exactly they should return is in the comments in the code.
Task is in the comments in the code below – at the top of the file and at the places marked TODO.
Loading the interactive part…
Six small functions for working with numbers and text – exactly the kind you need when processing forms. Functions come in the next lesson, so they are prepared for you: you only change the places with TODO and the comment next to them says what value the line should produce. Under the editor you'll see which functions already work.
Task is in the comments in the code below – at the top of the file and at the places marked TODO.
Loading the interactive part…
const everywhere, let only when you reassign the value, var never. A variable is valid in its block { … }.Array.isArray.0.1 + 0.2). Round with Math.round(x * 100) / 100, toFixed returns text. Write them for people with toLocaleString('en-US').`…${x}…` instead of joining with plus. String methods return a new text.Number() and watch out for NaN.===, never ==.