Ausnahme gefangen: SSL certificate problem: certificate is not yet valid ๐Ÿ“Œ Scoping in JavaScript

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š Scoping in JavaScript


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

When coding in JavaScript, tracking the scope of your variables and functions are critical to being able to write clear, concise and re-usable code. Scope refers to the rules in JavaScript that determine when and where variables and functions are accessible within your code. There are three main types of scoping in JavaScript; global scope, function scope and block scope.

Global Scope

When writing in JavaScript, any variable or function declared outside of a function or block of code is said to be โ€œin global scope.โ€ This means that it can be accessed, or โ€œcalledโ€, from anywhere within the code for functions or anywhere after the variable declaration for variables. This difference is because functions are โ€œhoistedโ€, or available in any part of the code as a result of the way JavaScript executes code. Because of this general availability at any time in your code, it may seem that global scope is the answer for every variable or function, however scoping every variable and function in global scope can lead to issues with recurrent variable or function names and ultimately make code harder to read and maintain. Because of this, it is usually advantageous to attempt to scope functions and variables to the parts of the code where they are needed to avoid conflicts within your code.

const apple = document.getElementById('apple')
function logApple(){
    console.log(apple)
}

The variable "apple" is available to the function logApple() because both are declared in global scope. This means that the variable "apple" can be accessed anywhere in the script after the variable declaration. Because of this fact, the variable "apple" does not need to be passed to the function logApple() as it already has access to the variable declared in global scope.

Function Scope

Because of the conflict issue with global scope, function scope is often used to preserve the functionality of code while reducing the possibility of naming conflicts or unintended code execution. Variables or functions initialized within the body of a function are said to be in โ€œfunction scopeโ€ and are only accessible to the function they are declared within, as well as any nested functions beneath the original function. Declaring variables and nested functions this way is ideal because it can help avoid naming conflicts. If two variables exist within a single JavaScript document with the same name but are declared within different functions then no naming conflict error occurs.

A function scoped variable is created in memory when the function is executed, and subsequently destroyed upon completion of that function. Therefore, two functions can exist and run within the same script, both declare identically named variables with different values, and not conflict. Though function scope allows for generally cleaner code and better organization through simpler naming conventions, using variables in this scope means that in order to access them within other functions we must pass them as arguments. Sometimes, this can make it difficult to follow where a variable originates or where the current value of that variable is assigned, or even cause hard to catch errors when you forget to pass a variable to a function that requires it!

function logApple(){
    const apple = document.getElementById('apple')
    console.log(apple)
}
console.log(apple)

In this example, the variable "apple" is declared in function scope, or within the body of the function logApple(). As a result, the variable apple is available to the console.log() within the function, however it is not available to the console.log() outside of the function, which will cause an error.

Block Scope

Block scope is a relatively new introduction to JavaScript, as it is an additional functionality added to the language alongside the let and const keywords. Where previously variables were only ever declared with the var keyword, and when declared outside of a function were always in global scope, variables declared with let or const are able to be confined to a โ€œblockโ€. This is accomplished by simply enclosing the code which you wish to be inside of the block in a set of curly braces({}). This announces to JavaScript that the code within is a โ€œblockโ€, and therefore any variables declared with let or const will only be accessible within that block. This can be useful when writing larger scripts where there may be naming conflicts, and also making your code more modular and easier to update.

{
const apple = document.getElementById('apple')
function logApple(){
    console.log(apple)
}
}
console.log(apple)

The code inside the block in this example is identical to the code in the first example and will work the same way. The variable "apple" is available to the function logApple() because they are both declared within the same block of code. The console.log() outside of the block will throw an error because it cannot access a variable declared within a block using let _or _const.

Ultimately, scoping is a core concept to JavaScript as a whole. Having a clear understanding of the uses and unintended effects of global scope and function scope, and the knowledge of how and when to utilize block scope is critical when attempting to write clear, clean, organized and maintainable code. Through proper application of all three types of scope, youโ€™ll be able to write code with great naming conventions and no collisions, keep everything well organized, and make your code easier for yourself and others to read, understand, update and adapt!

Sources:
This guy

...



๐Ÿ“Œ How Variable Scoping Works in JavaScript


๐Ÿ“ˆ 33.78 Punkte

๐Ÿ“Œ Scoping in JavaScript


๐Ÿ“ˆ 33.78 Punkte

๐Ÿ“Œ Scoping & Planning a Network Pentest Tips | Rules of Engagement | InfoSe...


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ ImmuniWeb Discovery to Intelligently Automate Penetration Testing Scoping and Scheduling


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ The Importance of Properly Scoping Cloud Environments


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ CSS Variables Scoping to Create and Theme Flexible UI Components


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ Lexical Scoping vs Closures


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ CSS Scoping in Svelte and How to Work Around It


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ Comment on PCI DSS Scoping and Segmentation by Infosec123 | Pearltrees


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ The Joys Of Scoping pt. 2 - Steve Levinson - SCW #11


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ The Joys Of Scoping - Steve Levinson - SCW #11


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ Scoping web application and web service penetration tests, (Mon, Aug 10th)


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ Enjin: Lack of Tenant Scoping Enables Limited Cross-Tenant Data Querying and Mutation


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ V8 Internals for JavaScript Developers (Make Your JavaScript Faster)


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ JavaScript: Dirty Parts of the Language (aka Maneuvering JavaScript Errors)


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ JavaScript: Dirty Parts of the Language (aka Maneuvering JavaScript Errors)


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ A Brief History of JavaScript by the Creator of JavaScript


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Why Is JavaScript So Fast? (aka JavaScript Engines - How Do They Even?)


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Dynamic Rendering for JavaScript web apps - JavaScript SEO


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Top 10 JavaScript Vulnerabilities (aka OWASP Top 10 for JavaScript Developers)


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ HTML and Javascript Teacher - Code examples in HTML and Javascript.


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Javascript: Neue Facebook-Webseite setzt komplett auf Javascript


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Low CVE-2020-14063: Tc custom javascript project Tc custom javascript


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Demo: Data types in JavaScript [16 of 51] | Beginner's Series to JavaScript


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Demo: Objects in JavaScript [44 of 51] | Beginner's Series to JavaScript


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Running JavaScript: browser or server [3 of 51] | Beginner's Series to JavaScript


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Mozilla Firefox up to 13.0 JavaScript SandBox Utility javascript: URL memory corruption


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ JavaScript-Experte Crockford: "Wir sollten nicht immer noch JavaScript nutzen"


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ State of JavaScript: Entwicklung und Akzeptanz von JavaScript im Jahr 2022


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Introduction to JavaScript: What is JavaScript and Why is it Important for Web Development?


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Mastering JavaScript Screen Capture: Essential Tips for Taking Screenshots | JavaScript Projects


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ How To Create a Calculator Using HTML CSS & JavaScript | Simple Calculator in JavaScript


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ Day-10 of Learning JavaScript: Whip Up Weather Wonders: Craft Your Own City-Ready Weather App with JavaScript Magic!


๐Ÿ“ˆ 14.92 Punkte

๐Ÿ“Œ JavaScript Tutorial Series: Introduction to JavaScript events.


๐Ÿ“ˆ 14.92 Punkte











matomo