For a long time, I struggled to learn React.
Because I didn't master JavaScript.
So, I am starting this series to help any JavaScript newbie master the language.
Ready? Let's go πͺ.
Anatomy of a JavaScript (JS) program
Let's look at the following JavaScript program π:
let selectedPost = undefined;
const id = Symbol("id");
const me = {
name: "Ndeye Fatou Diop",
age: 30,
[id]: "123",
hasPremium: true,
rank: 25n,
linkedAccounts: null,
};
const greet = (user) => console.log(`Hello ${user.name}.
You are ${user.age > 18 ? "major" : "minor"}.`);
greet(me); // prints "Hello Ndeye Fatou Diop. You are major."
Every JavaScript program is a series of commands
for the machine (browser, computer, etc.). In the example above, the browser will print Hello Ndeye Fatou Diop. You are major.
on the console when the program is done executing.
Every program is made out of:
1. Comments
Comments help you document your code or note things to do later. They are ignored by the browser or machine and are only for humans.
There are 2 kinds of comments:
1. Inline comments
// This is a single-line comment
2. Multiline comments
/*
This is a comment
that spans multiple lines.
Great for making long code clear!
*/
Even though computers ignore comments, some editors, like VSCode, can use special comments like JSDoc to give helpful hints.
2. Values
A value is any information the code uses, such as numbers or text. Each value has a type or data type that tells JavaScript what it is.
Our code has different values: undefined,
30
, true
,β¦
undefined // "undefined" type
"Ndeye Fatou Diop" // "string" type
Symbol("id") -> "symbol" type
me // "object" type
30 // "number" type
true // "boolean" type
25n // "bigint" type
null // "null" type
greet // "object" type
π‘ You can find a value's type using
typeof value
, except fornull
, which gives "object". Watch out for that! π
3. Variables
Variables are like labels for values. They let us work with data by giving it a name.
Think of variables as people's names. You're not your name, and when I say things like ${PutYourNameHere} is late
or Give 100$ to ${PutYourNameHere}
, I am referring to you.
There are 3 ways to declare a variable in JavaScript:
- Using
let
: Variables declared withlet
can be re-assigned (i.e., we can change the value stored in the variable)
let name = "Ndeye Fatou Diop";
name = "Joyancefa"; // We can change the value stored in `name`
- Using
const
: Variables declared withconst
cannot be re-assigned (i.e., once the variable is assigned, we cannot re-assign it)
const name = "Ndeye Fatou Diop";
name = "Joyancefa"; // β Uncaught TypeError: Assignment to constant variable.
- Using
var
: Don't use this! It's the old way of declaring variables and has some unexpected behaviors. Variables declared withvar
can also be re-assigned.
var name = "Ndeye Fatou Diop";
name = "Joyancefa"; // We can change the value stored in `name`
π‘ When a value is written as is in the code without any changes, it's called a literal. In the example above,
30
,"Ndeye Fatou Diop"
,β¦ are literals.
4. Operators
Operators let you perform operations on one or more values. There are different types of operators:
-
unary operators:
typeof
,!
,--
, ... -
binary operator:
=
,+
,%
,... -
ternary operator:
condition? exprIfTrue : exprIfFalse
- etc.
In the example above, we use the ternary operator to print the correct string when greeting me.
console.log(`Hello ${user.name}. You are ${user.age > 18 ? "major" : "minor"}.`
π‘ You can learn more about operators here.
5. Keywords
Keywords are special words with specific meanings in JavaScript. For example, for
is a keyword used in loops.
Some are reserved
(i.e., you can't use them for variable names).
Even if not all of them are (e.g., undefined
), avoiding using keyword names for variables is good practice.
π‘ You can find the list of reserved keywords more here. No need to remember them all! Your code editor should yell at you when you use them.
6. Expressions
Expressions are bits of code that produce a value. Any expression can be formed of operators, values, variables, expressions, etc.
In the example above, we have the following expressions:
- All the values mentioned earlier (
30
,true
,β¦) - All the assignments (
selectedPost = undefined
,id = Symbol("id")
,β¦) user.name
${user.age > 18 ? "major" : "minor"}
Hello ${user.name}. You are ${user.age > 18 ? "major" : "minor"
(user) => console.log(...)
greet(me)
π‘ Check this great post to learn more about the distinction between
statements
andexpressions
.
7. Statements
A statement is just a collection of expressions. It is an instruction to the program to do something like an order in the real world.
Examples
return
for...of
if...else
do...while
Statements cannot be used when expressions are expected (for example, in function arguments).
β Bad: This won't work
function greet(name) {
return "Hello, " + name;
}
// Incorrect usage: Using a statement (if) instead of an expression
greet(if (Math.random() < 0.5) { "John" } else { "Jane" }); // Uncaught SyntaxError: Unexpected token 'if'
The example above has 5 statements (one on each line).
π‘ The semi-colon after the statement is optional, but I prefer to add it to keep the code clear.
Find the complete list of statements here.
Thank you for reading this post π.
Leave a comment π© to share what you think.
And Don't forget to Drop a "ππ¦π₯".
If you like articles like this, join my FREE newsletter, FrontendJoy.
If you want daily tips, find me on X/Twitter.
Top comments (5)
All of your content, AMAZING! I love your summaries. I am a newbie and your content is helping me so much!! Much gratitude to you and all the time you put into this and every one of your posts. You could write a book.
Thank you so much for the appreciation π
It definitely gives me the courage to continue βΊοΈ
This is where it all starts...
Merci Nd.Fatou !!!
Glad you like it Benoit π