Fundamentals of JavaScript Part-2

NAZLICAN ŞAHİN
4 min readJan 24, 2024

--

A brief intro to the syntax of JavaScript

In this little introduction I want to tell you about 5 concepts:

  • white space
  • case sensitivity
  • literals
  • identifiers
  • comments

4.1. White space

JavaScript does not consider white space meaningful. Spaces and line breaks can be added in any fashion you might like, even though this is in theory.

In practice, you will most likely keep a well-defined style and adhere to what people commonly use, and enforce this using a linter or a styling tool such as Prettier.

For example, I like to always use 2 characters to indent.

4.2. Case sensitive

JavaScript is case-sensitive. A variable named something is different from Something.

The same goes for any identifier.

4.3. Literals

We define as literal a value that is written in the source code, for example, a number, a string, a boolean or also more advanced constructs, like Object Literals or Array Literals:

///

4.4. Identifiers

An identifier is a sequence of characters that can be used to identify a variable, a function, or an object. It can start with a letter, the dollar sign $ or an underscore _, and it can contain digits. Using Unicode, a letter can be any allowed char, for example, an emoji 😄.

////

The dollar sign is commonly used to reference DOM elements.

Some names are reserved for JavaScript internal use, and we can’t use them as identifiers.

4.5. Comments

Comments are one of the most important parts of any program. In any programming language. They are important because they let us annotate the code and add important information that otherwise would not be available to other people (or ourselves) reading the code.

In JavaScript, we can write a comment on a single line using //. Everything after // is not considered as code by the JavaScript interpreter.

Like this:

///

Another type of comment is a multi-line comment. It starts with /* and ends with */.

Everything in between is not considered as code:

////

Every line in a JavaScript program is optionally terminated using semicolons.

I said optionally, because the JavaScript interpreter is smart enough to introduce semicolons for you.

In most cases, you can omit semicolons altogether from your programs.

This fact is very controversial, and you’ll always find code that uses semicolons and code that does not.

My personal preference is to always avoid semicolons unless strictly necessary.

6. Values

A hello string is a value. A number like 12 is a value.

hello and 12 are values. string and number are the types of those values.

The type is the kind of value, its category. We have many different types in JavaScript, and we’ll talk about them in detail later on. Each type has its own characteristics.

When we need to have a reference to a value, we assign it to a variable. The variable can have a name, and the value is what’s stored in a variable, so we can later access that value through the variable name.

7. Variables

A variable is a value assigned to an identifier, so you can reference and use it later in the program.

This is because JavaScript is loosely typed, a concept you’ll frequently hear about.

A variable must be declared before you can use it.

4 Ways to Declare a JavaScript Variable:

•var Declares a variable

•let Declares a block variable

•const Declares a block constant

or nothing

Note: You cannot re-declare a variable declared with let or const. This will not work:

My advice is to always use const and only use let when you know you’ll need to reassign a value to that variable. Why? Because the less power our code has, the better. If we know a value cannot be reassigned, it’s one less source for bugs.

Now that we saw how to work with const and let, I want to mention var.

Until 2015, var was the only way we could declare a variable in JavaScript. Today, a modern codebase will most likely just use const and let. There are some fundamental differences which I detail in this post but if you’re just starting out, you might not care about them. Just use const and let.

What is the Differences Between let and var

They are nearly same but not literal.

Redeclaring a JavaScript variable with var is allowed anywhere in a program:

With let, redeclaring a variable in the same block is NOT allowed:

Redeclaring a variable with let, in another block, IS allowed:

Everything clear I hope😁 So wait for part 3!

--

--

No responses yet