Skip to content

Saadkhan0105/JavaScript-HC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

JAVASCRIPT

Variable and Data:

  • Just like we follow some rule while speaking english (the grammar) we have some rules to follow while writing a javascript program.
  • The set of those rules is called syntax in javascript.

Variable:

  • A variable is a container that stores a value.
  • This is very similar to the containers used to store rice, water and oats (treat this as an analogy).
  • The value of a javascript variable can be changed during the execution.
  • In javascript, we have two types of variables: var and let.

var:

  • var is a global variable and can be used anywhere in the program.
  • It is declared using the keyword var followed by the variable name and the value.
  • It can be updated and re-declared within its scope.
  • It is initialized with undefined.
var a = 7; 

let:

  • let is a block-scoped variable and can be used only within the block it is defined.
  • It is declared using the keyword let followed by the variable name and the value.
  • It can be updated but not re-declared within its scope.
  • It is not initialized with undefined.
let a = 7;

const:

  • const is a block-scoped variable and can be used only within the block it is defined.
  • It is declared using the keyword const followed by the variable name and the value.
  • It can be updated but not re-declared within its scope.
  • It is not initialized with undefined.
const a = 7;

Rules of Assigning a Varibale:

  • Letters, digits, underscore and $ sign is allowed.
  • Must begin with a $, _ or a letter.
  • Javascript reserved words cannot be used as a variable name.
  • Saad and sAAd are different variables (case sensitive).

Data Types:

  • In javascript, there are several data types such as string, number, boolean, object, array, function, etc.
  • We can use the typeof operator to check the data type of a variable.
typeof "Hello World" // "string"
typeof 10 // "number"
typeof true // "boolean"
typeof {} // "object"
typeof [] // "object"
typeof function() {} // "function"

var VS let in Javascript:

  • var is glovaly scoped While let & const are block scoped.
  • Var can be updated and re-declared within its scope.
  • let can be updated but not re-declared.
  • const can neither nor be re-declared.
  • var variables are initialized with undefined whereas let and const variables are not initialized.
  • const must be initialized during declaration unlike let and var.

Interview Questions:

  1. What is the difference between var, let, and const in JavaScript?
  2. What is the difference between undefined and null in JavaScript?

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published