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

let name = "The Web"; const galaxy = "Milky Way"; var age = 100; name = "Nebula"; // let and var can be reassigned
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

var name = "Linc"; var age = 120; var bool = true; var arr= ["The", "Web"]; arr[0]; // returns "The"arr[1] = "The"; // arr is now ["The", "The"] var obj = {name: "The", age:100}; obj.age; // returns 100
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.

Operators

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(!)

Arrays

var arr= [1, 2, 3]; // or var arr = new Array(1, 2, 3); arr[1] = 10; // returns [1, 10, 3] arr[0] // returns 1
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.

Objects

var obj = {name : "The Web", age : 100} obj.age // returns 100 obj["galaxy"] = "Milky Way"; // obj is {name: "The", age: 100; galaxy: "Milky Way"}
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).

Statement

if(condition){  //code }else if(another condition){  //code }else{  //code }
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.
switch(expression) {  case x:    // code block    break;  case y:    // code block    break;  default:    // code block }
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.

Loops

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.
while (condition) {  // code }
The while loop iterates over a block of code as long as a condition is met.
do {  // code block to be executed  continue; }while (condition);
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.
for (initialization; condition; incre/decrement)  {      // code to be executed      break; }
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.

Function

function func_name(para1, para2) {  // code to be executed  return }
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.
badge