Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cheatsheet and Challenges

NotificationsYou must be signed in to change notification settings

KaarthiikM/JavaScript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 

Repository files navigation

Variable Declaration
Using Var,let,const
Scope,Hoisting
Re-declaration and Re-assignment

var,let, andconst are keywords used for variable declaration in JavaScript. They differ in terms of scope, hoisting, and reassignment capabilities. Here's an explanation of each:

  1. var:

    • Variables declared withvar are function-scoped or globally scoped, but not block-scoped. This means they are accessible throughout the entire function or globally if declared outside any function.
    • Variables declared withvar are hoisted to the top of their scope, which means you can use them before they're declared, but their value will beundefined.
    • var variables can be redeclared and reassigned.
    functionexample(){varx=10;if(true){varx=20;// This changes the value of the outer x as well}console.log(x);// 20}
  2. let:

    • Variables declared withlet are block-scoped, which means they're only accessible within the block they're defined in.
    • let variables are not hoisted, so you can't use them before they're declared.
    • let variables can be reassigned but not redeclared within the same scope.
    functionexample(){lety=30;if(true){lety=40;// This doesn't affect the outer yconsole.log(y);// 40}console.log(y);// 30}
  3. const:

    • Variables declared withconst are block-scoped, likelet.
    • const variables must be assigned a value at the time of declaration and cannot be reassigned.
    • const variables are not hoisted and have the same scoping rules aslet.
    functionexample(){constz=50;if(true){constz=60;// This doesn't affect the outer zconsole.log(z);// 60}console.log(z);// 50}

For modern code, it's generally recommended to uselet andconst overvar. Uselet when you need to reassign a variable's value, and useconst when the value shouldn't change after assignment. This practice helps improve code readability and maintainability.


Primitive DataType->Are Immutable->Checks Value
NumberStringBoolean
UndefinedNullSymbol

Primitive data types are the building blocks for more complex data structures and values in JavaScript. Unlike objects, primitive values are immutable, which means they cannot be changed directly; any operation on them creates a new value rather than modifying the existing one. Checks value not reference unlike non-primitive.

In JavaScript, there are six primitive data types:

  1. Number:Can be int,float,negative,exponential, "NaN", "Infinity".

    constage=-25;constprice=-99.99;letscientificNum=1.23e6;// 1.23 * 10^6 (1230000)letpositiveInfinity=Infinity;console.log(positiveInfinity);// Infinityconsole.log(-10/0);// -Infinityconsole.log(1e308*2);// Infinityletresult1=0/0;// NaN(Not a Number)letresult2="abc"*2;// NaNletresult3=Math.sqrt(-1);// NaNletnanResult=NaN*10;// NaNletanotherNan=NaN+5;// NaN
  2. String:Represents sequences of characters enclosed in single ('') or double ("") quotes.
    Strings are immutable

    constname="Alice";constmessage='Hello, '+name;

    templating string:

    console.log(`This template string by${name}`);console.log(`${5+3}`);
  3. Boolean:Represents true or false values.

    constisStudent=true;consthasPermission=false;
  4. Undefined:Represents a variable that has been declared but hasn't been assigned a value.

    letx;// x is undefined
  5. Null:Represents the intentional absence of any value or object.

    constnoValue=null;
  6. Symbol:Represents a unique and immutable value, often used as object property keys.

    constid=Symbol("unique identifier");

Type Conversions

  1. String to Number:

    letstr="42";letnum=Number(str);// Converts string to number 42
  2. Number to String:

    letnum=42;letstr=String(num);// Converts number to string 42
  3. Boolean to String:

    letbool=true;letstr=String(bool);// Converts boolean to string ("true" or "false")
  4. String to Boolean:

    letstr="true";letbool=Boolean(str);// Converts string to boolean (true if string is not empty)
  5. Number to Boolean:

    letnum=0;letbool=Boolean(num);// Converts number to boolean (false if number is 0, true otherwise)
  6. Object to String:

    letobj={key:"value"};letstr=String(obj);// Converts object to string "[object Object]"
  7. String to Object:

    letstr="{ key: 'value' }";letobj=JSON.parse(str);// Converts JSON string to object
  8. Number to Object:

    letnum=42;letobj=newNumber(num);// Converts number to Number object
  9. Array to String:

    letarr=[1,2,3];letstr=arr.toString();// Converts array to comma-separated string "1,2,3"
  10. String to Array:

    letstr="1,2,3";letarr=str.split(",");// Converts comma-separated string to array [1, 2, 3]

parseInt()Number()
parseInt("123");// Output: 123parseInt("010");// Output: 10 (interpreted as decimal, not octal)parseInt("0xA");// Output: 10 (interpreted as decimal)parseInt("12.34");// Output: 12 (fractional part is ignored)
Number("123");// Output: 123Number("010");// Output: 10 (interpreted as decimal)Number("0xA");// Output: NaN (not a valid decimal number)Number("12.34");// Output: 12.34

About

Cheatsheet and Challenges

Topics

Resources

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp