Simple Data Types
Strings are essentially a "static" series of characters of any kind, surrounded by either single quotes, or double quotes. They don't really have a size limit, but by default they got to be on a single line.
Strings look like this:
let myString = "I'm a string."; const otherString = 'This is also a string'; // If you're using the double or single quotes they have to be "escaped": const escapedSingle = 'I\'m cool and it\'s fine!'; const escapedDouble = "This is an \"escape\" from real life"; // You can put strings together, called "concatenating" using a + sign: const concatString = escapedSingle + " I'm in the middle " + escapedDouble; // You can also append a string with the special += method: myString += " and I've added to myString now!";
Single, Double, Backtick???
The above code example shows the use of both single and double quotes and how to escape them, but the other thing you can do is also use the "opposite" quote to simplify string.
const str1 = 'This is "a string" with no escapes'; const str2 = "Let's do it the other way!";
So what about the backtick? You might have seen those. They're used in a different concept called Template Literals. They're used to merge things together, but they're a bit more complicated and deserve their own page, so head on over there to learn more about them!
Integers and Numbers
const myInt = 42; // note the lack of any quotes! const myFloat = 346.32462346; // You can do some math with JS, of course. The language follows regular math order // of operation, parentheses go first, then * and / , etc. const myResult = 10 * (33 - 5) / 2 + 8; // result: 148
0.1 + 0.2will actually not equal
A boolean is essentially a value that is true or false. Defining a boolean is essentially super easy:
const imTrue = true; const imFalse = false;
Now, that's not extremely useful to be honest, but booleans are much, much more than that. There are many operations that will return a boolean value when you execute them. Here are a few examples:
const isBigger = 5 > 2; // true const isEqual = "blue" === "red"; // false
Some things are not quite booleans but will act the same. I will reserve this explanation for conditions however, which are explored in another part of this guide. That's also where we'll talk about those operators I just used!
Null and Undefined
Some values can be either null or undefined, so these two types are considered data types, but are generally used under very specific circumstances.
let test; // test is "undefined" but won't crash the bot console.log(test); // undefined console.log(x); // crashes with "ReferenceError: x is not defined; const what = null; // valid console.log(what); // null