
x32x01
أدارة أكتب كود
- بواسطة x32x01 ||
كيف تنشئ مصفوفة في جافاسكريبت؟

مثال:
JavaScript:
let arr1 = [1, 2, 3];
let arr2 = new Array(1, 2, 3);
. ما هو الفرق بين push وunshift؟

push يُضيف عنصرًا إلى نهاية المصفوفة.
unshift يُضيف عنصرًا إلى بداية المصفوفة.
مثال:
JavaScript:
let arr = [2, 3];
arr.push(4); // [2, 3, 4]
arr.unshift(1); // [1, 2, 3, 4]
كيف يمكنك إزالة عنصر من مصفوفة؟

pop () تزيل آخر عنصر في المصفوفة
Shift() تزيل أول عنصر في المصفوفة
مثال:
JavaScript:
let arr = [1, 2, 3];
arr.pop(); // [1, 2]
arr.shift(); // [2, 3]
كيف تتحقق مما إذا كان عنصر ما موجودًا في مصفوفة؟

مثال:
JavaScript:
let arr = [1, 2, 3];
arr.includes(2); // true
arr.indexOf(2); // 1 (رقم ال index )
كيف تقوم بترتيب العناصر في مصفوفة؟

يمكن تخصيص الترتيب بتمرير دالة مقارنة.
مثال:
JavaScript:
let arr = [3, 1, 4, 1, 5];
arr.sort(); // [1, 1, 3, 4, 5]
JavaScript:
arr.sort((a, b) => b - a); // [5, 4, 3, 1, 1] - ترتيب تنازلي
ما هي slice وsplice؟

slice تُعيد جزءًا من المصفوفة بدون تعديل المصفوفة الأصلية.
splice تقوم بإزالة أو استبدال عناصر من المصفوفة الأصلية.
مثال:
JavaScript:
let arr = [1, 2, 3, 4];
let sliced = arr.slice(1, 3); // [2, 3]
JavaScript:
arr.splice(1, 2); // [1, 4] (تمت إزالة العنصرين الثاني والثالث)
كيف يتم تحويل مصفوفة إلى سلسلة نصية؟

مثال:
JavaScript:
let arr = [1, 2, 3];
let str = arr.join("-"); // "1-2-3"
كيف يمكنك عكس ترتيب العناصر في مصفوفة؟

مثال:
JavaScript:
let arr = [1, 2, 3];
arr.reverse(); // [3, 2, 1]
كيف يتم دمج مصفوفتين؟

مثال:
JavaScript:
let arr1 = [1, 2];
let arr2 = [3, 4];
let merged = arr1.concat(arr2); // [1, 2, 3, 4]
let merged2 = [...arr1, ...arr2]; // [1, 2, 3, 4]
كيف يمكن إزالة عناصر مكررة من مصفوفة؟

مثال:
JavaScript:
let arr = [1, 2, 2, 3, 3, 3];
let uniqueArr = [...new Set(arr)]; // [1, 2, 3]
كيف يمكنك إيجاد أول عنصر يحقق شرطًا معينًا؟
الجواب/ باستخدام الدالة find، التي تُعيد أول عنصر يحقق الشرط.مثال:
JavaScript:
let arr = [1, 2, 3, 4];
let firstEven = arr.find(x => x % 2 === 0); // 2
ما هي أنواع الحلقات في JavaScript؟

for: تستخدم عند معرفة عدد التكرارات مسبقًا.
while: تستخدم عندما ترغب في تكرار الكود طالما أن الشرط صحيح.
do...while: تشبه حلقة while ولكن يتم تنفيذ الكود مرة واحدة على الأقل قبل التحقق من الشرط.
for...in: تستخدم لتكرار خصائص الكائنات (objects).
for...of: تستخدم لتكرار العناصر في المصفوفات أو أي كائن قابل للتكرار.
. ما الفرق بين حلقة for وحلقة while ؟

for: غالبًا ما تستخدم عندما يكون عدد التكرارات محددًا أو معروفًا مسبقًا.
while: تستخدم عندما يكون الشرط غير معروف مسبقًا، وتستمر في التكرار طالما أن الشرط صحيح.
كيف يمكن (إيقاف) الحلقة؟

يمكنك استخدام الأمر break لإيقاف الحلقة تمامًا. على سبيل المثال:
JavaScript:
for (let i = 0; i < 10; i++) {
if (i === 5) {
break; // ينهي الحلقة عند i = 5
}
console.log(i);
}
كيف تتخطى تكرارًا واحدًا في الحلقة دون ايقافھا ؟

JavaScript:
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue; // يتخطى i = 2 ويكمل
}
console.log(i);
}
كيف يمكن استخدام forEach للتكرار على عناصر المصفوفة؟

JavaScript:
const numbers = [1, 2, 3];
numbers.forEach(function(number) {
console.log(number);
});
ما هي الدالة (Function) في JavaScript؟

JavaScript:
function greet() {
console.log("مرحباً!");
}
greet(); // تستدعى هنا الدالة
كيف يمكن تعريف دالة في JavaScript؟

JavaScript:
function sum(a, b) {
return a + b; // إرجاع مجموع القيمتين
}
كيف يتم استدعاء دالة؟

JavaScript:
sum(3, 5); // الناتج 8
ما الفرق بين الدوال العادية (Regular Functions) والدوال السهمية (Arrow Functions)؟

JavaScript:
function sayHello(name) {
return مرحباً، ${name}!;
}
الدوال السهمية:
JavaScript:
const sayHello = (name) => مرحباً، ${name}!;
هل يمكن لدالة أن تكون داخل دالة أخرى؟

JavaScript:
function outerFunction(x) {
function innerFunction(y) {
return x + y;
}
return innerFunction;
}
const add5 = outerFunction(5);
console.log(add5(10)); // الناتج 15
ما الفرق بين return و console.log؟

return: تُستخدم لإرجاع قيمة من الدالة. عند استخدام return، يتوقف تنفيذ الدالة.
console.log: تُستخدم لطباعة قيمة في وحدة التحكم، لكنها لا تُرجع أي شيء.
JavaScript:
function sum(a, b) {
return a + b; // ترجع المجموع
console.log("لن يتم تنفيذ هذا الكود");
}
هل يمكن لدالة أن تستدعي نفسها؟

JavaScript:
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(5)); // الناتج 120
ما الفرق بين الدالة المُعرّفة (Function Declaration) والدالة المُخزّنة (Function Expression)؟

Function Declaration:
JavaScript:
function greet() {
console.log("مرحباً!");
}
Function Expression:
JavaScript:
const greet = function () {
console.log("مرحباً!");
};
كيف يمكن لدالة قبول عدد غير معروف من المعاملات؟

JavaScript:
function sumAll(...numbers) {
return numbers.reduce((sum, num) => sum + num, 0);
}
console.log(sumAll(1, 2, 3, 4)); // الناتج 10
ما هي الـ Arrow Function؟

كيف تختلف الـ Arrow Function عن الدوال التقليدية؟

لا يمكن استخدامها كـ constructor (أي لا تدعم new).
لا تحتوي على الكلمة المفتاحية arguments.
ما هو الشكل العام للـ Arrow Function؟

JavaScript:
const add = (a, b) => a + b;
// مع قوسين
const greet = (name) => Hello, ${name}!;
// بدون قوسين (إذا كان هناك معامل واحد فقط)
const square = x => x * x;
// بدون قوسين وبدون أقواس الكتل (إذا كان التعبير من سطر واحد)
هل يمكن كتابة دوال متعددة الأسطر باستخدام Arrow Function؟

JavaScript:
const multiply = (a, b) => {
const result = a * b;
return result;
};
ما هي الـ Higher-Order Function؟

لماذا تُستخدم الـ Higher-Order Functions؟

لتجنب التكرار.
لإدارة العمليات المعقدة باستخدام دوال بسيطة مثل map, filter, و reduce.
أعطِ مثالًا لدالة Higher-Order Function.

JavaScript:
const numbers = [1, 2, 3, 4];
const squared = numbers.map(num => num * num);
console.log(squared); // [1, 4, 9, 16]


map تُرجع مصفوفة جديدة بناءً على المصفوفة الأصلية.
forEach تنفذ وظيفة على كل عنصر دون إرجاع قيمة جديدة.
كيف يعمل reduce؟

JavaScript:
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((accumulator, current) => accumulator + current, 0);
console.log(sum); // 10
هل يمكن أن تكون Higher-Order Function دالة تُرجع دالة؟

JavaScript:
const createMultiplier = multiplier => number => number * multiplier;
const double = createMultiplier(2);
console.log(double(5)); // 10
const triple = createMultiplier(3);
console.log(triple(5)); // 15


كيف تُستخدم Arrow Functions مع Higher-Order Functions؟

JavaScript:
const names = ['Ali', 'Sara', 'John'];
const greetings = names.map(name => Hello, ${name}!);
console.log(greetings); // ["Hello, Ali!", "Hello, Sara!", "Hello, John!"]
كيف يساعد الجمع بينهما في كتابة كود مختصر؟

JavaScript:
const evenNumbers = [1, 2, 3, 4, 5, 6].filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4, 6]
ما هو الكائن (Object) في JavaScript؟

JavaScript:
let person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello!");
}
};
كيف يمكن إنشاء كائن في JavaScript؟

1. باستخدام المعامل {}:
JavaScript:
let obj = {};
2. باستخدام Object Constructor:
JavaScript:
let obj = new Object();
3. باستخدام Object.create:
JavaScript:
let obj = Object.create(null);
ما الفرق بين خاصية ووسيلة في الكائن؟

الوسيلة (Method): هي دالة مرتبطة بالكائن.
JavaScript:
let car = {
brand: "Toyota", // خاصية
start: function() { // وسيلة
console.log("Car started");
}
};
كيف يمكن الوصول إلى خصائص الكائن؟

1. باستخدام النقطة (Dot Notation):
console.log(car.brand);
2. باستخدام الأقواس المربعة (Bracket Notation):
console.log(car["brand"]);
كيف يمكن إضافة أو تعديل خصائص الكائن؟

JavaScript:
let obj = {};
obj.name = "John"; // إضافة
obj.name = "Doe"; // تعديل
كيف يمكن حذف خاصية من الكائن؟

JavaScript:
delete obj.name;
console.log(obj.name); // undefined
. ما هو الفرق بين Object.keys و Object.values و Object.entries؟

Object.keys: تُرجع جميع المفاتيح (keys) كصفيف.
Object.values: تُرجع جميع القيم (values) كصفيف.
Object.entries: تُرجع مصفوفة تحتوي على أزواج المفتاح والقيمة.
JavaScript:
let obj = { a: 1, b: 2 };
console.log(Object.keys(obj)); // ["a", "b"]
console.log(Object.values(obj)); // [1, 2]
console.log(Object.entries(obj)); // [["a", 1], ["b", 2]]
ما هو الفرق بين hasOwnProperty و in؟

hasOwnProperty: يتحقق مما إذا كانت الخاصية موجودة مباشرة في الكائن نفسه (وليس في البروتوتايب).
in: يتحقق مما إذا كانت الخاصية موجودة في الكائن أو في سلسلة البروتوتايب.
JavaScript:
let obj = { a: 1 };
console.log(obj.hasOwnProperty("a")); // true
console.log("a" in obj); // true
كيف يمكن نسخ كائن؟

JavaScript:
let obj1 = { a: 1 };
let obj2 = Object.assign({}, obj1);
باستخدام Spread Operator:
JavaScript:
let obj1 = { a: 1 };
let obj2 = { ...obj1 };
ما هو الفرق بين النسخ السطحي (Shallow Copy) والنسخ العميق (Deep Copy)؟

النسخ العميق: ينسخ الكائن بالكامل بما في ذلك الكائنات المتداخلة.
JavaScript:
let obj1 = { a: 1, b: { c: 2 } };
let shallowCopy = { ...obj1 }; // نسخ سطحي
let deepCopy = JSON.parse(JSON.stringify(obj1)); // نسخ عميق
ما هو Object.freeze و Object.seal؟

Object.freeze: يجعل الكائن غير قابل للتعديل (لا يمكن إضافة أو تعديل أو حذف الخصائص).
JavaScript:
let obj = { a: 1 };
Object.freeze(obj);
obj.a = 2; // لن يتغير
Object.seal: يمنع إضافة أو حذف خصائص جديدة، لكن يمكن تعديل الخصائص الحالية.
JavaScript:
let obj = { a: 1 };
Object.seal(obj);
obj.a = 2; // ممكن
delete obj.a; // غير ممكن
ما هو this داخل كائن؟

JavaScript:
let obj = {
name: "John",
greet: function() {
console.log(this.name);
}
};
obj.greet(); // "John"
كيف يمكن دمج كائنين أو أكثر؟

باستخدام Object.assign:
JavaScript:
let obj1 = { a: 1 };
let obj2 = { b: 2 };
let merged = Object.assign({}, obj1, obj2);
باستخدام Spread Operator:
JavaScript:
let merged = { ...obj1, ...obj2 };
ما هو البروتوتايب (Prototype) في JavaScript؟

JavaScript:
let obj = { a: 1 };
console.log(obj.proto); // Object prototype
كيف يمكن التحقق مما إذا كان كائن هو بروتوتايب لكائن آخر؟

JavaScript:
let obj1 = {};
let obj2 = Object.create(obj1);
console.log(obj1.isPrototypeOf(obj2)); // true
ما هو الفرق بين Object.defineProperty و Object.defineProperties؟

Object.defineProperty: تُستخدم لتحديد خاصية واحدة بكائن مع خيارات التحكم.
Object.defineProperties: تُستخدم لتحديد خصائص متعددة.