#2 Variables and data types | Javascript Hindi Course For Beginners ( 2023 )

 

What is syntax ?

Syntax is like grammar for computer code. Just as grammar rules tell you how to put words and sentences together correctly in a language, syntax rules tell you how to write code correctly in a programming language.
The proper use of syntax is necessary for the code to be understood and executed by the computer. A small error in syntax, such as a missing semicolon or an incorrect operator, can cause the code to not work as intended.

What is variables ?

A variable is like a container that holds a value. It's like a box where you can put a number, a word, or a group of words, and then give the box a name. Later on, you can use that name to refer to the value inside the box.
 
For Example→ if you get assignment or homework from collage or school
🕵️ Brilliant Student completes that assignment and writes all answers in paper sheet. And that paper sheet is used by every student to create own copy.
Paper sheet is like variable where information is stored. And that information is used by every student to create there own copies.
notion image

Rules for Creating Variables

  1. Variable names must start with a letter, underscore (_), or dollar sign ($). They cannot start with a number.
  1. Variable names can only contain letters, numbers, underscores (_), and dollar signs ($). They cannot contain spaces or special characters.
  1. JavaScript is case-sensitive, so "variable" and "Variable" are two different variables.
  1. Variables must be declared before they are used with the keywords "var", "let" or "const"
  1. It's a good practice to give variables meaningful names that describe the value they hold.
 

var vs let vs const

1. "var" is the oldest way to declare a variable in JavaScript and it is function-scoped which means only accessible within the function it is declared. 2. "let" is a newer way to declare a variable in JavaScript and it is block-scoped which means it is only accessible within the block of code it is declared. 3. "const" is used to declare a variable with a constant or fixed value and it is block-scoped and the variable cannot be reassigned but the properties can be changed in case of objects and arrays. 4. Variables declared with "var" and "let" are hoisted, which means they can be accessed before they are declared. 5. Variables declared with "const" cannot be reassigned but the properties can be changed in case of objects and arrays 6. It's recommended to use "let" when you plan to reassign a variable and "const" when you don't. 7. "const" variable declaration is more clear and ~safe as it cannot be reassigned by mistake. 8. Variables declared with "var" and "let" can be re-declared multiple times in the same scope but "const" variable cannot be re-declared.
 

Variable Scopes

In JavaScript, there are two main types of variable scopes: global and local.
  1. Global scope: Variables that are defined outside of any function or block can be accessed anywhere in the program.
  1. Local scope: Variables that are defined inside a function or a block can only be accessed within that function or block.
Additionally, there are some more scopes like function scope, block scope, lexical scope and closure scope which are the advanced concepts of javascript.
It's important to understand these scopes to make sure that your variables are being used correctly and not causing any unintended behavior in your code.
 

Reserved keywords

JavaScript has several reserved keywords that have specific meanings and cannot be used as variable or function names. Some of the most commonly used reserved keywords in JavaScript include:
  • var
  • let
  • const
  • function
  • if
  • else
  • for
  • while
  • do
  • break
  • continue
  • return
  • switch
  • case
  • default
  • try
  • catch
  • finally
  • throw
  • in
  • instanceof
  • new
  • typeof
  • this
  • null
  • undefined
  • true
  • false
 
 

Primitive data types

 
  1. Number: Numbers can be integers (e.g. 1, -5) or floating-point values (e.g. 3.14, -1.6). JavaScript uses a single type to represent all numbers, so there's no distinction between integers and floating-point values.
  1. String: A string is a sequence of characters, enclosed in single or double quotes (e.g. "hello", 'world').
  1. Boolean: A boolean value can be either true or false.
  1. Undefined: A variable that has been declared but has not been assigned a value is undefined.
  1. Symbol: A Symbol is a new type introduced in ECMAScript 6, its unique and immutable primitive value.
  1. BigInt: The BigInt is a new type introduced in ECMAScript 2020, It is used to represent integers of arbitrary length.
These types are the building blocks of JavaScript and are used to create more complex data structures like arrays and objects.

Primitive value in JavaScript.

  1. NaN (Not-a-Number) is a special value that represents the result of an undefined or unrepresentable operation, such as the square root of a negative number or the result of dividing 0 by 0. It is considered a primitive value in JavaScript.
  1. null is a special keyword in JavaScript that represents an intentional non-value or an empty value. It is often used to represent the absence of an object or variable. It is considered a primitive value in JavaScript.
Both NaN and null are special values in JavaScript, but they are not primitive data types like number, string, boolean, symbol and bigint. They are considered primitive values because they are not objects and have special behavior.

Reference data types

 
In JavaScript, the data types that are not considered primitive are called "non-primitive" or "reference" data types. These types include:
  1. Object: Objects are used to store collections of key-value pairs and can represent any type of data, including other objects, functions, and arrays.
  1. Array: Arrays are used to store ordered collections of data. They are a type of object and can store any type of data, including other arrays, objects, and functions.
  1. Function: Functions are blocks of code that can be executed when called. They are a type of object and can be stored in variables, passed as arguments, and returned from other functions.
  1. Map: A Map is a new data structure introduced in ECMAScript 6, it stores a collection of key-value pairs like an object but it has some additional features and it's iterable.
  1. Set: A Set is a new data structure introduced in ECMAScript 6, it stores unique values of any type and it's iterable.
  1. WeakMap and WeakSet: WeakMap and WeakSet are special kind of Map and Set. They allow you to store key-value pairs and unique values respectively, but these keys and values are weakly held, meaning they can be garbage collected if there are no other references to them.
All these types are objects, and they are created by using the built-in constructor functions and literals. They have their own properties and methods and are used to represent more complex data structures in javascript.
Questions: