Just JavaScript

"Exploring the Language from Basic"

Just JavaScript

As in my previous blog "Simplified JavaScript" you have seen why JavaScript is important and how it has become an integral part of web development. I have mentioned its functions and its features as a programming language so that before diving into its concepts one should know about what this language is about and how any beginner should deal with its learning process. So now let's start with some of its basic concepts.

Syntax and Basic Concepts:

Here we will discuss 5 concepts:-

  • White Space: White space in JavaScript refers to the empty spaces, tabs, and line breaks used to format and separate code. While it doesn't affect code execution, proper white space usage improves code readability and organization. Consistent indentation and spacing make code easier to understand and maintain.

  • Case Sensitive: JavaScript is a case-sensitive language. For example, a variable named 'Hello' is different from 'hello'.

  • Literals: In JavaScript, literals refer to fixed values that are directly written into the code. They represent specific types of data, such as numbers, strings, booleans, arrays, objects and more.

    Literals are used to assign values to a variable.

      Number literal:123
      String literal:"Hello World!"
      Boolean literal:true
      Array literal:[1,2,3]
      Object literal:{name:"Aish", age:26}
    

    It provides a convenient way to represent and work with data directly in the code. They are fixed values that are not stored in a variable or dynamically computed. The value of a literal remains constant throughout the program's execution.

    It should be noted that literals are different from variables. Variables can hold different values throughout the programs' execution, whereas literals represent specific fixed values that do not change.

  • Identifier: It's a sequence of characters that can be used to identify a variable, a function, or an object. It can start with a letter, $ sign or underscore and it can contain digits.

      let name = "John";
      let age = 30;
      console.log("My name is " + name + "and I am" + age + "years old.");
    

    In the above code, name and age are identifiers to represent variables that store the values "John" and 30 respectively.

Comments: It is used to add explanatory text within the code. They are ignored by the JavaScript interpreter and do not affect the execution of the code. It provides information to the developers, making the code easier to understand and maintain. There are two types of comments:

Single line comments

Multi-line comments

//This is a single line comment
let name = "John"
/* This is a multi-line comment.
You can give explanation of the code here using multi line comments
*/
let age = 30; //Assigning value
console.log("My name is" + name + "and I am" + age "years old.");

Variables:

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

There are 3 ways to declare a variable in JavaScript:

  1. var: The var keyword was traditionally used to declare variables in JS. Variables declared with var are function-scoped i.e. they are accessible within the function they are declared in or globally if declared outside the function.

  2. let: The let keyword was introduced in ECMAScript 6(ES6), allows the declaration of block-scoped variables. They are accessible within the block they are declared in or within the function if declared inside a function.

const: The const keyword is used to declare constants, which are variables whose values cannot be reassigned once they are assigned. It is also block-scoped.

const does not mean "constant" in the way some other languages like C mean. It does not mean the value cannot be changed, it means it cannot be reassigned. If the variable point to an array or object the content of the object or the array can freely change. It must be initialized at the declaration time while let can be initialized later.

//Global scope
var x = 5;
function example() {
  //function scope
  var y = 10;

 if(x === 5) {
    //Block scope
    let z = 15;
    console.log(z); //Output : 15
 }
console.log(y); //Output : 10
console.log(z); //This will throw error because 'z' is not accessible here
}
example();
console.log(x); //Output : 5
console.log(y); //Output : Error as 'y' is not accessible here

Data Types:

Variables in JS do not have any type attached. They are untyped.

In JS, data types can be broadly categorized into two main groups: primitive types and object types.

  1. Primitive Types: They are basic data types that store single values. They are immutable. They are passed by value when assigning or passing them as arguments to functions. There are six primitive types n JS: string, number, boolean, null, undefined, and symbol.

  2. Object Types: They are complex data types that store collections of key-value pairs, methods and properties. Objects are mutable and are passed by reference when assigning or passing them as arguments to functions. Examples of object types include object, array, function, and Date.

Expressions:

In JS, an expression is a combination of values, variables, operators, and function calls that evaluates to a resulting value. It can be as simple as a single value or as complex as a combination of multiple operators.

An expression is a piece of code that produces a value when evaluated.

Examples of expressions include mathematical expressions, logical comparisons, string concatenation, and function invocations.

They are the building blocks of code that perform operations and produce results. They are essential for manipulating data and controlling program flow.

//Mathematical expressions
let sum = 5+3; //Addition expression
let product = 4*6; //Multiplication expression
let result = (sum+product) / 2; //Combination of expressions
console.log(result); //Output : 16

//String expressions
let greeting = "Hello";
let name = "John";
let message = greeting + " " + name; //String concatenation
console.log(message); //Output : "Hello John"

//Logical expressions
let isTrue = true;
let isFalse = false;
let isBothTrue = isTrue && isFalse; //Logical AND expression
let isEitherTrue = isTrue || isFalse; //Logical OR expression
console.log(isBothTrue); //Output: false
console.log(isEitherTrue); //Output : true

//Function invocation expressions
function multiply(a,b) {
  return a*b;
}
let productresult = multiply(3,4); //Invoking function
console.log(productResult); //Output : 12

Operators:

It allows you to get two simple expressions and combine them to form more complex expressions.

  1. Arithmetic operator (+, -, *, /, %)

  2. Assignment operator (=, +=, -=, *=)

  3. Logical operator(&&, ||)

  4. Comparison operator(==, !=, >, >=, <=, <)

The topics above explained are some basic concepts about JavaScript which will be helpful for you to understand the nuisances of the advanced topics.