Episode 2: JavaScript Data Types and Syntax: A Comprehensive Guide for Web Developer

Episode 2: JavaScript Data Types and Syntax: A Comprehensive Guide for Web Developer

In this chapter we are going to learn everything about syntaxes and datatypes of JavaScript

ยท

4 min read

๐Ÿ‘จโ€๐Ÿ”ง Syntax in JavaScript

So first let's understand what is syntax.

Syntax means a set of rules which are used to construct programs in javascript. Let's understand it with a real-life example.

Suppose we want to make tea, now what are the rules to make tea?

So if we want to make tea then first we take a bowl then some sugar, water and
Tea leaf. After that, we boil all of these things for some time. Now our tea is ready for enjoyment. Same as tea if we want to make some programming statements in any programming language then we have to follow their syntaxes and rules.

Here is a detailed example of javascript syntaxes

๐Ÿ‘‰ Syntax examples

If we want to create a variable in JavaScript How we can do this let's see...

Variables

// create variable
var x;
let marks;
const name;

// assign values to the varialble
var x = 20;
let marks = 89.30;
const name = "Raj Patel";

// how to use variables?
let x = 20;
let y = 25;
let z = x + y;

console.log(z);  // it will print 45.

Data types

In javascript, there are a total of 8 datatypes-

  1. String

  2. Number

  3. Bigint

  4. Boolean

  5. Undefined

  6. Null

  7. Symbol

  8. Object

In another programming language like C or C++, we have to tell the compiler, what types of values, like string, boolean, integer, float etc, we want to store in the variables but in javascript, we don't need to do this. Javascript understands itself which types of values are contained by the variables.

Look at these examples below-

let a = "Hello JavaScript"; // string
let b = 20; // number
let c = true; // boolean
let d = null; // null
let e = undefined // undefined
let f = ["Raj", "Patel", 20, true]; // array
let g = {name: "Raj Patel", age: 22}; // object

So, as we can see here we don't need to specify a data-types, int, char, float etc like others in C or C++. Javascript identifies itself with the help of assigned values.

JavaScript Values

There are total 2 types of values defined by javascript syntax

  • Fixed values

  • Variable values

Fixed value: Fixed value means a value that remains constant and can't be changed during the execution of the program.

This mostly refers to a constant value and these are created by using the const keyword.

// for example
const PI = 3.14;

Here PI is defined using the const keyword and the value has been already assigned. Now if we try to assign a new value, It will throw an error.

Variable value: Variable value means a value that can be changed during the execution of the program.

These types of variables are created by using the let and var keywords.

// for example
let counter = 0;
counter = 1;

Now at the execution time, the counter value will be 1. So it is an example of variable value.

JavaScript Literals

In simple words, the literal represents values in javascript and these values are fixed.

Here are some most common types of literals-

  1. Numeric Literals: They represent numbers in decimal, hexadecimal, octal, or binary formats. For example:

     let decimalNum = 40;
     let hexNum = 0x2a;
     let octalNum = 052;
     let binaryNum = 0b101010;
    
  2. String Literals: They represent sequences of characters enclosed with (" ") or (' ').

    For example:

     let singleQuote = 'Hello, JavaScript!';
     let doubleQuote = "Hello, JavaScript!";
    
  3. Boolean Literals: These represent the two Boolean values - true or false. For example:

     let boolTrue = true;
     let boolFalse = false;
    
  4. Array Literals: These represent a list of values enclosed within square brackets []. For example:

     let myArray = [1, 2, 3, 'Raj Patel', true];
    
  5. Object Literals: These represent an unordered collection of key-value pairs enclosed within curly braces {}. For example:

     let myObject = {
       name: 'Raj Patel',
       age: 22,
       isStudent: true
     };
    

Comments in JavaScript

So, if we want that some portions of code should not execute by the compiler then we use comments in js.

Comments are useful when we want to document our code properly.

There are 2 types of comments present in javascript-

  1. Single-line comment:

     // This is a single line comment and // this is a symbole.
    
  2. Multi-line comment:

     /*  This is a 
     multiline comment
     as you can see i am writing
     this sentences in multiple lines */
    

๐Ÿ‘‹ Outro

So that's all guys, hope you understood all concepts which I mentioned above. Till then keep learning.

Bye...

ย