Chuck's Academy

Basic JavaScript

JavaScript Fundamentals

In this chapter, we will cover the essential fundamentals of JavaScript, which will help you understand how this language works at a basic level. We will learn about the language's syntax, the data types you can handle, the variables, and the operators that allow you to perform operations on these data.

Basic Syntax

JavaScript, like other programming languages, has its own syntax, a set of rules that determine how code should be written and structured. Here are some key points of the syntax:

  • Each JavaScript statement generally ends with a semicolon ;, although it is not strictly necessary in many cases.
  • Braces {} are used to group code blocks, for example in functions or control structures.
  • Single-line comments begin with //, while multi-line comments are enclosed between /* and */.
javascript
"Here we see examples of how to write comments and declare a variable using let. Comments can be single-line with double slash or multi-line using slash asterisk. It also shows how to assign the value five to a variable called number."

Data Types and Variables

JavaScript has several data types that you can use to store information. The most common data types are:

  • String: for text.
  • Number: for numbers (integers and decimals).
  • Boolean: for true or false values.
  • Null: represents the intentional absence of a value.
  • Undefined: a declared variable but with no assigned value.

Variable Declaration

In JavaScript, variables can be declared using the keywords var, let, or const. The main differences are scope and whether the value can change.

javascript
"Here we declare three variables: one using let called name with the value John, another with const called pi which has the value three point fourteen sixteen, and one more with var called age which has the value thirty. We use let when we want to reassign a value, const when we want a constant variable, and var when we want a variable with a greater scope."

Value Assignment

A variable is assigned a value using the assignment operator =. The value can be any data type, such as a number or a text string.

javascript
"Here we are assigning the value Hello World to a variable called message and the value ninety-five to a variable called score. Variables in JavaScript can hold text, numbers, or many other data types."

Operators

Operators allow you to perform operations on variables and values. In JavaScript, there are several types of operators:

Arithmetic Operators

These operators are used to perform mathematical operations:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
javascript
"In this example, we declare two variables called a and b with the values ten and twenty respectively. We then add these two variables and store the result in a new variable called sum, which has the value thirty. We also subtract a from b and store the result in difference, which equals ten."

Comparison Operators

Comparison operators allow you to compare two values. They return true or false depending on whether the condition is true.

  • == Equality
  • === Strict equality (checks data type and value)
  • != Inequality
  • <, >, <=, >= Numeric comparisons
javascript
"Here we have two variables, x with the value ten and y with the value ten but as a text string. Using the double equal operator we check that the values are equal, so it returns true. However, the triple equal checks both the value and the data type, so it returns false because one is a number and the other is text."

Functions

A function in JavaScript is a block of code designed to perform a specific task. You can define your own functions using the keyword function, followed by the function name and parentheses ().

javascript
"Here we have defined a function called greet that simply prints the message Hello World to the console. We then call the function using greet with parentheses."

You can also create functions that take parameters and return a value:

javascript
"This function called sum takes two parameters, a and b, and returns the sum of these. We call the function with the values five and ten, and store the result in a variable called result, which has the value of fifteen."

Conclusion

In this chapter, we have covered the foundational concepts of JavaScript, from its basic syntax to the use of variables, data types, operators, and functions. These are the pillars upon which your JavaScript knowledge will be built."


Ask me anything