Hello, friends! In this article, we will take a brief look at the fundamentals of JavaScript.
In this article, we'll cover:
- JavaScript Statement Concept
- JavaScript White Space Usage
- JavaScript Code Blocks
- Usage of JavaScript Keywords
- JavaScript Syntax Concept
- JavaScript Immutable and Mutable Concepts
- Data Types in JavaScript
- JavaScript Variables
- JavaScript Operators
- Concept of JavaScript Expressions
- Concept of JavaScript Identifiers
- JavaScript Case Sensitivity
- Summary
I will briefly touch on them, and in the coming days, I will elaborate on the topics.
I hope you enjoy reading.
Yazının Türkçe versiyonu için linke tıklayabilirsiniz.
A computer program creates expressions that will be executed by the computer. In programming languages, these are called statements. A JavaScript application consists of a list of statements.
In JavaScript, statements can consist of variables, values, operators, expressions, keywords, and comments.
Example
/* The line below carries the statement feature. */
var x;
JavaScript statements are executed in the order they are written.
Example
console.log("This will run first.");
console.log("Then this will run.");
console.log("Finally, this will run.");
This will run first.
Then this will run.
Finally, this will run.
The ;
symbol is used to separate statements from each other. A single line can contain more than one statement.
Example
/* The example below shows three statements created in a single line. */
console.log("1.Statement"); console.log("2.Statement"); console.log("3.Statement");
1.Statement
2.Statement
3.Statement
JavaScript ignores white space characters. This means that statements written on a single line have the same meaning as statements created on multiple lines.
💡 To ensure code readability, it is recommended to write each statement on a separate line. Keeping each line to around 80 characters on average can enhance readability.
Example
let x = 3, let y = 4;
/* The above expression can also be written as follows. */
let x = 3;
let y = 4;
In JavaScript, {}
braces are used to group code. Grouped code blocks are referred to as methods or functions.
Example
function drive() {
console.log("Let's ride George");
}
// The expression "Let's ride George" is printed to the console.
drive();
Let's ride George
JavaScript expressions often begin with a keyword.
❗ Keyword names cannot be used when defining variable or method names because these keywords are reserved in JavaScript.
Example
/* ❌ The variable declaration below is incorrect. */
let var = 5;
// The statement "SyntaxError: Unexpected token 'var'" will be printed to the console.
console.log(var);
The commonly used keywords are listed below.
Keyword | Description |
---|---|
var |
Creates a variable. |
let |
Creates a block-scoped variable. |
const |
Creates a block-scoped constant variable. |
I will touch upon terms and usage types as we go along. I will explain the purposes of keywords like var
, const
, let
and their differences in the process.
Syntax in JavaScript determines the arrangement of code. We can think of these rules as the writing guide or grammar of a language.
Example
// We see the syntax used to create a variable in the example.
var x;
var y;
"Osman" = let z;
/**
* ❌ The syntax of the z variable is incorrect. The statement "SyntaxError: Invalid
* left-hand side in assignment" will be printed to the console.
*/
console.log (z);
In JavaScript, variables are divided into two groups based on data types:
-
Immutable (Constant values)
-
Mutable (Variable values)
Constant values are also called Literals. Each assigned value has a new address in memory (RAM), and the content of the assigned value cannot be changed in memory.
💡 We generally use constant values in places where we want to preserve the original data. For example, the original data may be used in multiple places. Data that is not preserved can lead to undesired results in the program.
Variable values are also referred to as variables. The assigned value has an address in memory, and the content of the assigned data can be changed. Therefore, they have the property of being references. When a new data is assigned to a variable-valued variable, a new space is not used in memory. The relevant reference address where the data is stored is found, and the old data is overwritten, saving the new data to the reference address.
💡 Variable-valued values do not take up space in memory like constants, as they use reference addresses for data. Therefore, if performance is a priority, we can use this data type.
Example
Below is an example of the immutable property.
// The studentName variable has the immutable property.
let studentName = "Emin";
console.log(`The content of the studentName variable is: ${studentName}`);
// We store the content of the studentName variable in the personName variable.
let personName = studentName;
/**
* We store a new value in the studentName variable. In this case, a new address will be allocated in RAM for
* Hasan.
*/
studentName = "Hasan";
console.log(
`Pay attention here. The value stored in the personName variable is: ${personName}`
);
console.log(`The content of the studentName variable is: ${studentName}`);
The content of the studentName variable is: Emin
Pay attention here. The value stored in the personName variable is: Emin
The content of the studentName variable is: Hasan
Below is an example of the mutable property.
Example
// Our variable 'vehicle' has mutable properties.
let vehicle = { type: "car", color: "orange" };
console.log(`The expression ${vehicle["type"]} is printed to the console.`);
// We create a variable named 'bus' and set its value to be referenced by the 'vehicle' variable.
let bus = vehicle;
// We access the 'type' key of the 'bus' variable and store a new value.
bus["type"] = "long bus";
console.log(`The expression ${bus["type"]} is printed to the console.`);
/**
* ⚠️ Pay attention here. The value stored in 'vehicle[type]' will be overwritten with "long bus,"
* and the console will print the expression "long bus."
*
* This is because when we change the content of the 'bus' variable, we simultaneously change the
* content at the reference address in memory that holds the data.
*/
console.log(`The expression ${vehicle["type"]} is printed to the console.`);
The expression car is printed to the console.
The expression long bus is printed to the console.
The expression long bus is printed to the console.
In JavaScript, there are two main types of data:
-
Primitive Data Types
-
Object Data Types
Primitive data types consist of number, string, boolean, undefined, null, symbol, and bigint. Except for the null data type, these are also characterized by their immutable nature.
Object data types consist of object, array, date and function. These data types, on the other hand, exhibit the mutable property.
It's important to understand that primitive data types are immutable, meaning their values cannot be changed once they are assigned. In contrast, object data types are mutable, allowing their values to be modified. This distinction plays a crucial role in understanding how data is handled and manipulated in JavaScript.
Variables in programming languages are used to store data. In JavaScript, variables can be declared using the var
, const
or let
keywords.
Example
// We stored the number 4 in the variable x.
var x = 4;
// We stored the expression "Hasan" in the variable y.
let y = "Hasan";
// We stored the number 3.14 in the constant variable pi.
const pi = 3.14;
console.log(`Value of x: ${x}`);
console.log(`Value of y: ${y}`);
console.log(`Value of pi: ${pi}`);
Value of x: 4
Value of y: Hasan
Value of pi: 3.14
In JavaScript, variables have dynamic type, meaning a variable can be used to hold values of different data types.
Example
// The data type of the variable x is undefined.
let x;
// The data type of the variable x is number.
x = 5;
// The data type of the variable x is string.
x = "Sebile";
console.log(`Value of x: ${x}`);
Value of x: Sebile
Essentially, we can use mathematical expressions that we use in everyday life within JavaScript. These are expressions such as ( + - * / )
and are referred to as arithmetic operators.
=
sign functions as an assignment operator, meaning it does not imply mathematical equality. For equality operation ==
or ===
operators are used. Data type of these operators will be boolean.
Example
// We assigned the value 4 to the variable x. The reference x is now storing the value 4.
let x = 4;
console.log(`Value of x:${x}`);
Value of x:4
In JavaScript, there are many operators. I will touch upon them as needed.
In JavaScript, the calculation of an operation in a single line is referred to as an expression. An expression consists of variables, values, and operators.
Example
var x = 4;
var y = 3;
// Expression
var z = 4 * 3;
Identifiers are used to name variables or functions in programming.
When naming variables in JavaScript, the following points should be noted:
-
In JavaScript, an identifier can start with an uppercase or lowercase letter,
$
, or_
. -
JavaScript identifiers are unique, meaning the same name cannot be used for another variable or method.
-
JavaScript is case-sensitive, so
x
andX
represent different variables. -
Numeric values cannot be the initial character when defining an identifier, but they can be used in other parts of the identifier.
-
Reserved keywords in JavaScript cannot be used as identifiers.
Example
/**
* ✔️ Proper naming conventions. ⚠️ Even though the variable names in the first two lines are the same,they
* define different variables.
*/
var deneme;
var Deneme;
var DENEME01;
var $deneme;
var _deneme;
// ❌ Incorrect naming conventions
var 1deneme;
// The 'let' keyword cannot be used as a variable name.
var let;
JavaScript is case-sensitive, meaning it distinguishes between uppercase and lowercase letters. firstname
and firstName
do not have the same meaning.
-
character cannot be used in JavaScript due to it being reserved.
In this section, we covered the fundamentals of JavaScript. We explored concepts such as JavaScript statements, white space usage, code blocks, keyword usage, syntax, immutable and mutable concepts, data types, and variables. Additionally, we touched on JavaScript operators, expressions, identifiers, and the distinction between uppercase and lowercase letters.
This foundational knowledge provides a basis for understanding JavaScript programming. In the upcoming sections, we will delve deeper into each topic, providing more detailed explanations and examples.
Top comments (0)