Build: The Web

Basic JavaScript

The variety of data types that a programming language offers is one of its most fundamental features. These are the kind of values that a programming language can represent and modify. Strings (wrapped in single or double quotes), numbers, big ints, Booleans (a value that is true or false), null, undefined, objects, and symbols are just a few of the data types available in JavaScript.


Variables are data storage containers. Name, galaxy, and age are variables in this example, and they are declared with the keywords let, const, and var. Variable names can contain letters, numerals, underscores, and dollar signs, and they must be unique. A letter, a dollar symbol ($), or an underscore (_) must be the first character in a name. The equal symbol (=) in JavaScript is an "assignment" operator, not an "equal to" operator.

More on variables

name is a string variable, age is a number, bool is a boolean, arr is an indexed array with zero as the index or position of the first item, and obj is an object with a key and a value.


Arithmetic Operators - add(+), subtract(-), multiply(*),divide(/)
Comparison Operators - equal to(==), not equal to(!=), greater than or equal to(>=), less than equal to(<=), greater than(>), less than(<)
Logical Operators - and (&&), or(||), not(!)


An Array object in JavaScript can store a list of variables. Use the [] or the Array object notation to define an array. To address a specific cell in an array, we can use the brackets [] operator. Because zero-based indices are used in addressing, the second component of myArray can be addressed with index 1. You can store components of multiple types in the same array since JavaScript Arrays are essentially special sorts of objects.


Members of objects are addressed using the brackets operator [} similar to arrays, but the period (.) operator can also be used, like in many other object-oriented languages. Except for the fact that brackets return a member using a string, but the period operator needs the member to be a plain word, they are quite similar (the word should not contain spaces, start with a number or use illegal characters).


The conditional statements in JavaScript are as follows:
If a certain condition is true, use if to provide a block of code that will be run.If the initial condition is false, use else if to define a new one to test. If the same condition is false, else is used to define a block of code to be run.
The switch statement is used to take different actions depending on the circumstances. The following is how it works: Only one time is the switch expression evaluated. The expression's value is compared against the values in each circumstance. If a match is found, the relevant code block is run. If no match is found, the default code block is run.


If you want to run the same code multiple times with different values, loops come in handy. When working with arrays, this is very common.
The while loop iterates over a block of code as long as a condition is met.
A version of the while loop is the do while loop. This loop will run the code block once before verifying if the condition is true, and then it will loop again as long as the condition is true.
The for loop in JavaScript iterates the components for a set number of times. If the number of iterations is known, it should be used.
The break statement exits a loop. The continue statement skips one loop iteration.


A JavaScript function is declared with the function keyword, followed by a name, followed by parenthesis (). Letters, numerals, underscores, and dollar signs can all be used in function names (same rules as variables).
Parameter names separated by commas may be included in the parentheses: (para1, para2) The code to be run, by the function, is placed inside curly brackets: {} The values passed to the function when it is called are known as function arguments. The arguments (parameters) are treated as local variables within the function.
The function will cease operating when it reaches a return statement in JavaScript.