JavaScript Fundamentals: The Concepts Beginners Often Miss
Understand the JavaScript concepts that seem simple until they behave unexpectedly.

Variable declaration without a keyword in JavaScript
firsttName = "Ayan";
console.log(firsttName);
JavaScript will execute this script, even without declaring the variable with any keyword (var, let, or const).
JavaScript doesn’t create a variable in the current scope. Instead JavaScript will create a property on the global object.
This behavior depends entirely on whether your code is running in the default or Strict Mode.
Default mode
If the variable is declared without a keyword inside a function or a block, JavaScrit searches up the scope chain. If JavaScaript cann't find a declaration for that variable name anywhere, it creates a new property on the global object.
function myFunction() {
secretValue = "I am global!"; // No keyword used
}
myFunction();
console.log(window.secretValue); // Output: "I am global!"
console.log(secretValue); // Output: "I am global!"
// (Accessible anywhere)
Strict Mode
When strict mode is enabled in script, assigning a value to an undeclared variable throws a ReferenceError instead of silently making it global.
"use strict";
function myFunction() {
secretValue = "I am global!"; // ❌ ReferenceError: secretValue is not defined
}
myFunction();
Strict Mode in JavaScript
Strict Mode in JavaScript is a feature that helps catch common coding mistakes and implement a stricter set of rules to improve code quality.
It eliminates silent errors, prevents the use of unsafe actions, and improves performance optimization by JavaScript engines.
"use strict" directive
The "use strict" directive in JavaScript is used to enable strict mode. It introduces a short-list of variable names that are reserved for features that might be added to the language a bit later.
"use strict"
const interface = "video";
//Uncaught SyntaxError: Unexpected strict mode reserved word
const private = true;
//Uncaught SyntaxError: Unexpected strict mode reserved word
"use strict";
function showValue() {
x = 10;
}
showValue()
// ReferenceError: x is not defined
Without
"strict mode", JavaScript would implicitly create a property in global object at execution time.With
"strict mode", this results an error, enforcing proper variable declaration.
How to build a string
There is a need to add string in existing string, then you can use the += operator. This is helpful when you want to build upon a string by adding more text to it over time.
let greeting = 'Hello';
greeting += ', John!';
console.log(greeting); // "Hello, John!"
In this case, the original string of Hello is not modified, Instead greeting now references the new string of Hello, John!.
What Is the typeof null Bug in JavaScript?
There's a well-known quirk in JavaScript when it comes to
null.
Let's take a look at an example:
let exampleVariable = null;
console.log(typeof exampleVariable); // "object"
In this example, we have a variable called exampleVariable and have assigned it the value of null. But when we use the typeof operator, it returns the data type of it is object.
This is widely considered a bug in JavaScript, dating back to its early days. The reason for this behavior is rooted in the way JavaScript was originally designed.
When the language was first implemented, values like null were represented as a special type of object, leading to this unexpected result. Unfortunately, this has become a part of the language, and while it's confusing, it's something you'll need to be aware of.
typeof Operator
The typeof operator is used to check the data type of a variable. It returns a string indicating the type of the variable.
let age = 25;
console.log(typeof age); // "number"
let isLoggedIn = true;
console.log(typeof isLoggedIn); // "boolean"
What Is the prompt() Method, and How Does It Work?
prompt(message, default);
The prompt() method is an important part of JavaScript's interaction with the user. It’s one of the simplest ways to get input from a user through a small pop-up dialog box.
The prompt() method takes two arguments: The first one is the message which will appear inside the dialog box, typically prompting the user to enter information.
And the second one is a default value which is optional and will fill the input field initially.
So, what exactly does the prompt() method do? It opens a dialog box that asks the user for some input, and then it returns the text entered by the user as a string.
Here's an example of how it works.
<button id="prompt-btn">Show Prompt</button>
<p id="output"></p>
<script src="index.js"></script>
const btn = document.getElementById("prompt-btn");
const output = document.getElementById("output");
btn.addEventListener("click", () => {
const userName = prompt("What is your name?", "Guest");
output.textContent = "Hello, " + userName + "!";
});
In this example, when the user clicks on the button, the prompt() method displays a dialog box with the message What is your name? and an input field that initially contains the value Guest.
If the user types their name and presses "OK", the userName variable will store the entered value.
If the user presses "Cancel", the userName variable will be set to null.
null signifies that the user did not provide any input. The output paragraph will then display a greeting message using the provided name or null if the user canceled.
You will learn techniques to avoid displaying null when a user cancels the prompt in future blogs.
prompt() method will halt (stop) the execution of the script until the user interacts with the dialog box.This means the rest(reamining) of your JavaScript code won’t run until the user either provides input and clicks "OK", or cancels the prompt.
One other point to consider is that while prompt() is useful for quick testing or small applications, it's generally avoided in modern, complex web applications due to its disruptive nature and inconsistent behavior across different browsers.
What Is ASCII
In programming, understanding how characters are represented as numbers is fundamental. This is where ASCII comes in.
ASCII, short for American Standard Code for Information Interchange, is a character encoding (convert into a coded form) standard used in computers to represent text. It assigns a numeric value to each character, which is universally recognized by machines.



